Re: Creating a simple hitcounter for PHP web pages. (saves to files)



..oO(tony19760619@xxxxxxxxx)

To make this work on a single page you will need to create/modify 4
files.
So here goes...

Hmm ...

Create a file called counter.php and put following code in it:
<?php
//Counter
$count_my_page = ("hitcounter.txt");

Why the parentheses?

$hits = file($count_my_page);
$hits[0] ++;
$hit_count = $hits[0];
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");

Why the double quotes?

fclose($fp);
//echo $hits[0];

What about file locking and prevention of simple page reloads?

//Details to add to list
$count_my_page = ("hitcounter.csv");
$fp = fopen($count_my_page , "a");
$ip=$_SERVER[REMOTE_ADDR];

Missing single quotes.

$dnsname = gethostbyaddr($ip);
$referer=$_SERVER[HTTP_REFERER];
$page=$_SERVER[PHP_SELF];

Same here. Check your error_reporting, you'll get at least three
E_NOTICE errors.

$now = date( "Y/m/d H:i:s", time() );
fputs($fp , "$now,$hit_count,$page,$ip,$dnsname,$referer\r\n");
fclose($fp);
?>

Micha
.



Relevant Pages

  • Re: Creating a simple hitcounter for PHP web pages. (saves to files)
    ... What about file locking and prevention of simple page reloads? ... I am interested in knowing how can you "lock" the number of hits on page reloads. ... Every good host allows access to the server logs. ... I am interested in a database based solution. ...
    (alt.php)
  • Re: ISAM error related to Excel 2003 Jet Engine on Server 2003 SP2
    ... a missing single quote...any of them can kill ... You have to SUPER ANALLY examine your connection string. ... We have tried single quotes and doubles quotes on the extended ... Sticking OLEDB; on the front didn't work anywhere. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Creating a simple hitcounter for PHP web pages. (saves to files)
    ... What about file locking and prevention of simple page reloads? ... I am interested in knowing how can you "lock" the number of hits on page reloads. ... But the main question is still "why a hit counter"? ... Every good host allows access to the server logs. ...
    (alt.php)
  • Re: Creating a simple hitcounter for PHP web pages. (saves to files)
    ... What about file locking and prevention of simple page reloads? ... I am interested in knowing how can you "lock" the number of hits on page reloads. ... But the main question is still "why a hit counter"? ... Every good host allows access to the server logs. ...
    (alt.php)
  • Re: Creating a simple hitcounter for PHP web pages. (saves to files)
    ... What about file locking and prevention of simple page reloads? ... I am interested in knowing how can you "lock" the number of hits on page reloads. ... But the main question is still "why a hit counter"? ... Every good host allows access to the server logs. ...
    (alt.php)