Zend - The PHP Company




Counters

Add Code


Page Counter & Hit Logger  

Type: code fragment
Added by: quonsar
Entered: 02/03/2000
Last modified: 08/12/1999
Rating: **** (3 votes)
Views: 15013
Simple hit counter and hit logger. Use in prepend.php or paste at top of html file.


<?
$pagecount
=0;

// read from existing file in php-bin/counters/ named filename.ext.count
$counterfile=$DOCUMENT_ROOT "/php-bin/counters" $PHP_SELF ".count";

if(!
$fp=fopen($counterfile,"r+")):
    echo 
"Unable to open $counterfilen";
else:
    
$pagecount=intval(fgets($fp,10));
    
$pagecount++;
    
rewind($fp);
    
fputs($fp,strval($pagecount));
    
fclose($fp);
endif;
$time=time();

// connect to postgresql
if(!$linkid pg_pconnect("""""""""my.postgresql.db")):
    echo 
"PGSQL ERROR: ".pg_errormessage();
endif;

// stuff data in table
if(!pg_exec($linkid,"insert into mylog (stamp,doc,hostname,browser,referer) values ($time,'$REQUEST_URI','$REMOTE_HOST','$HTTP_USER_AGENT','$HTTP_REFERER')")):
        echo 
"PGSQL ERROR: ".pg_errormessage();
endif;

// output the count anywhere on the page
if($pagecount): echo $pagecount; endif;
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories