Re: Creating a simple hitcounter for PHP web pages. (saves to files)
- From: tony19760619@xxxxxxxxx
- Date: Fri, 30 May 2008 11:20:05 -0700 (PDT)
The idea was to make a simple counter using files...
To implement your request complicates things a lot.
But here is contents for the counter.php file for writing to csv file
<?php
//------------------------------
//-- Values you can set
//------------------------------
//This is number of seconds to try and open and lock
//the files needed for the counter to work
$seconds = 10; //10 is one second, so 20 is two
$counter_for_site = ("hitcounter.txt");
$list_for_site = ("hitcounter.csv");
//------------------------------
//-- Open and lock counter file
//------------------------------
$counter_file_open = False;
$counter_file_lock = False;
$list_file_open = False;
$list_file_lock = False;
for($counter=0;$counter <= $seconds;$counter+=1) {
//Open files
if($counter_file_open == False){
$fp_counter_file = fopen($counter_for_site , "r+");
if ($fp_counter_file != False){
$counter_file_open = True;
}
}
if($list_file_open == False){
$fp_list_file = fopen($list_for_site , "a");
if ($fp_counter_file != False){
$counter_file_open = True;
}
}
//Now lock the files
if($counter_file_lock == False){
$lock_state = flock($fp_list_file,LOCK_EX);
if ($lock_state == True){
$counter_file_lock = 1;
}
}
if($list_file_lock == False){
$lock_state = flock($fp_list_file,LOCK_EX);
if ($lock_state == True){
$list_file_lock = True;
}
}
if (($counter_file_lock == True) and ($list_file_lock == True)){
break;
}
sleep(0.1);
}
if (($counter_file_lock == False) or ($list_file_lock == False)){
fclose($fp_counter_file);
fclose($fp_list_file);
exit("Could not lock counter files");
}
//-- Info
//-- We need to suppress messages from file functions since PHP
//-- is not sure we using a valid file pointer
//------------------------------
//-- Do counter stuff
//------------------------------
//$hits = file($counter_for_site);
//$hits[0] ++;
//$hit_count = $hits[0];
$hits = @fgets($fp_counter_file);
$hits_i = (int)$hits;
$hits_i++;
$hits = (string)$hits_i;
@rewind($fp_counter_file);
@fputs($fp_counter_file , "$hits");
@fclose($fp_counter_file);
//------------------------------
//-- Do list details stuff
//------------------------------
$ip=$_SERVER[REMOTE_ADDR];
$dnsname = gethostbyaddr($ip);
$referer=$_SERVER[HTTP_REFERER];
$page=$_SERVER[PHP_SELF];
$now = date( "Y/m/d H:i:s", time() );
@fputs($fp_list_file , "$now,$hits,$page,$ip,$dnsname,$referer\r\n");
@fclose($fp_list_file);
?>
.
- Follow-Ups:
- Re: Creating a simple hitcounter for PHP web pages. (saves to files)
- From: Michael Fesser
- Re: Creating a simple hitcounter for PHP web pages. (saves to files)
- References:
- Creating a simple hitcounter for PHP web pages. (saves to files)
- From: tony19760619
- Creating a simple hitcounter for PHP web pages. (saves to files)
- Prev by Date: cookies problem
- Next by Date: Re: cookies problem
- Previous by thread: Re: Creating a simple hitcounter for PHP web pages. (saves to files)
- Next by thread: Re: Creating a simple hitcounter for PHP web pages. (saves to files)
- Index(es):
Relevant Pages
|