Mega Code Archive

 
Categories / Php / MySQL Database
 

Simple PHP-3 Access Counter (using GD and DBM functions)

<?php // // PHP/3 WebCounter Module // // // Parameters: // // digits=number_of_counter_digits (default: 6) // font=font_for_counter_digits (default: default) // key=key_for_counter_digits (default: HTTP_REFERER) // force=number_for_counter_digits (no default value) // // Installation: // // - Install the script in an appropriate directory. // - Install the font files (one GIF encoded image for each digit) // in subdirectories named after the fonts. // // Add to "srm.conf": // // Alias /digits/ /path/to/digits/ // // To enable PHP/3 for .php3 files add to "srm.conf": // // AddType application/x-httpd-php3 .php3 // // Usage: // // <IMG SRC="/digits/counter.php3?digits=7" ALT="[counter]"> // if( !isset($digits) ) { $digits = 6; } if( !isset($font) ) { $font = "default"; } if( !isset($key) ) { if( isset($HTTP_REFERER) ) { $key = $HTTP_REFERER; } else { $key = $PHP_SELF; } } $base = "./"; $dbase = $base."url.gdbm"; if( !file_exists($dbase) ) { $number = 0; } else { $dbm = dbmopen($dbase,"r"); $number = dbmfetch($dbm,$key); dbmclose($dbm); } $dbm = dbmopen($dbase,file_exists($dbase)?"w":"n"); if( isset($force) ) { dbmreplace($dbm,$key,(string)((int)$force)); } else { dbmreplace($dbm,$key,(string)(1+(int)$number)); } dbmclose($dbm); Header("Content-type: image/gif"); Header("Cache-control: no-cache"); Header("Pragma: no-cache"); Header("Last-modified: ".gmdate("D M d h:i:s Y",time())." GMT"); Header("Expires: ".gmdate("D M d h:i:s Y",time()+1)." GMT"); for( $i=0; $i<10; ++$i ) { $digit[] = ImageCreateFromGif($base.$font."/".((string)$i).".gif"); } $dx = ImageSX($digit[0]); $dy = ImageSY($digit[0]); $image = ImageCreate($digits*$dx,$dy); $number = sprintf("%0".((string)$digits)."d",(int)$number); for( $i=0; $i<$digits; ++$i ) { ImageCopyResized($image, $digit[(int)substr($number,$i,1)], $i*$dx,0,0,0,$dx,$dy,$dx,$dy); } for( $i=0; $i<count($digit); ++$i ) { ImageDestroy($digit[$i]); } ImageGif($image); ImageDestroy($image); ?>