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 ":)";
 
 
 
?>
 

Descargar Articulo y Ejemplos - Comentarios

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
 

Descargar Articulo y Ejemplos - Comentarios

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("33[".$x.";".$y."H");
}
 
function colorshell($c,$t,$f = 40)
{
        // Color
        // 30 - 37
        // Fondo 40-47
        return sprintf("%c[%d;%d;%dm".$t,27,1, $c ,$f);
}
 
 
 
echo colorshell(32,"Welcome to Testing Server ");
echo colorshell(35,"
 
                          /|_
                        ,'  .
                    ,--'    _,'
                   /       /
                  (   -.  |
                  |     ) |
                 (`-.  '--.)
                  `. )----'
");
 
echo colorshell(32,"ttt Miauuu ");
echo colorshell(37,"nr");
 
?>
 




Descargar Articulo y Ejemplos - Comentarios

Read data from excel file in php

Thanks to class Spreadsheet_Excel_Reader (Vadim Tkachenko) , we can easily read an Excel spreadsheet in PHP, one need only create an instance of the class and execute the method read (as a parameter giving the path of the excel spreadsheet) and the object will get all the data of the template in a var sheets. In the following example, we can see how easy it is to use Spreadsheet_Excel_Reader.

    <?php
// Example by deerme.org
// pear path, example /usr/share/php5
$pear = "./pear";
ini_set("include_path",ini_get("include_path").":$pear");
require_once 'excel/reader.php';
 
// Instance
$xlsreader = new Spreadsheet_Excel_Reader();
// Output Encoding.
$xlsreader->setOutputEncoding('CP1251');
// File
$xlsreader->read('prom-psu-2010.xls');
// Data
print_r( $xlsreader->sheets[0] );
// http://deerme.org
?>

Descargar Articulo y Ejemplos - Comentarios
 1 2 3 >  Last ›
Clean and Simple

Proyectos

Enlaces a mis classes,proyectos,ideas,etc para compartir xD

jQuery Powered

PHP

Contuct Us

Contact Us

info at deerme.org