



Implementación simple de un lock en php
La siguiente clase nos permite manejar un lock simple basado en un archivo de lock al cual le cambiamos la fecha de modificación. Util si es necesario asegurar que un proceso corra una sola vez.
<?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 ":)"; ?>
Generación de todas las combinaciones de un Alfabeto en PHP
La siguiente función recursiva permite generar todas las combinaciones posibles de un alfabeto dado. En el ejemplo veemos que utilizamos el alfabeto por defecto de la función y limitamos a un maximo de 6 caracteres (limite superior del bucle for).
<?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,''); } ?>
# Llamando al script por linea de comandos y guardando su salida en el archivo /tmp/mydic.txt php combinate.php > /tmp/mydic.txt
Apuntes de PHP GD
Recopilación de apuntes,tips y scripts de la libreria PHP GD para la generación de imágenes dinámicas.
1.- ¿ Como generar una grilla entre dos colores con 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"); ?>
Pronto mas material :)
Descargar Articulo y Ejemplos - Comentarios
Sequencia de color ANSI en PHP
Todas las terminales modernas soporte la sequencia de colores ANSI, con lo cual podemos hacer que nuestros scripts de consola sean mas divertidos llamativos xD utilizando colores para el texto o el fondo.
<? // Ansi Escape Sequences in PHP // by deerme.org function movecursor($x,$y) { return sprintf("