



Simple implementation of a lock in php
The following class allows us to handle a simple lock file based on a lock which will change the modified date. Useful if you need to ensure that a process runs once.
<?php // For PHP 5 or higher ( For the destroyer in the class ) // by deerme.org class lock { var $file; var $time_limit = 600; function lock( $file ) { if ( !is_writable(dirname( $file )) ) die("directory is not writable"); $this->file = $file; } function exits_lock() { if ( !is_file( $this->file ) ) { touch( $this->file ); return false; } else { // Check the time of lock $time=@filemtime( $this->file ); if($time && ($time<=(time()- $this->time_limit ))) { touch( $this->file ); return false; } else { return true; } } } function __destruct() { unlink( $this->file ); } } $mylock = new lock("/var/lock/myapp.lock"); if ( $mylock->exits_lock() ) die("app running"); sleep(5); echo ":)"; ?>
Generating all combinations of Alphabet in PHP
The following recursive function can generate all possible combinations of a given alphabet. In the example we use the alphabet veemos default function and limited to a maximum of 6 characters (upper limit of the loop).
<?php // How to generate all combinations of the alphabet // by deerme function generate_combinations_alphabet($width, $position, $base , $charset = 'abcdefghijklmnopqrstuvwxyz.-_1234567890') { for ($i = 0; $i < strlen( $charset ) ; ++$i) { if ($position < $width - 1) { generate_combinations_alphabet($width, $position + 1, $base.$charset[$i]); } print $base.$charset[$i]."n"; } } // the "for" limits the length of the combinations tods, this example all combinations of the alphabet by default with up to 6 characters for($i=0;$i<6;$i++) { generate_combinations_alphabet($i,0,''); } ?>
# Run script php combinate.php > /tmp/mydic.txt
Notes and Tips on PHP GD
Collection of notes, tips and scripts of PHP GD library to generate dynamic images.
1 .- How to generate a grid between two colors with PHP?
<?php // Generate a grid between two colors with PHP // by deerme.org $w = 1280; $h = 1280; $img = imagecreatetruecolor( $w , $h); $c1 = imagecolorallocate( $img , 255 , 0 , 255 ); $c2 = imagecolorallocate( $img , 255 , 255 , 255 ); imagefilledrectangle($img , 0,0,$w,$h,$c2); for( $i = 0 ; $i <= $w ; $i=$i+8 ) { for( $j=0;$j<=$h;$j=$j+8 ) { imagefilledrectangle($img , $i,$j,$i+8,$j+8, ( ( $count%2 == 0 ) ? $c1 : $c2 ) ); $count++; } } imagepng($img , "grilla.png"); ?>
Soon more material :)
Descargar Articulo y Ejemplos - Comentarios
Ansi Escape Sequences in PHP
All the modern terminals support ANSI color sequence, which we can make our shell scripts are more fun xD using flashy colors for text and background.
<? // Ansi Escape Sequences in PHP // by deerme.org function movecursor($x,$y) { return sprintf("