Jump to content
xisto Community
maddog39

Php Unique Hit Counter Count page hits with php.

Recommended Posts

A simple changePhp Unique Hit Counter

I made a few changes to the script above. Maybe some will find it helpful...

This script locks the file hints.Txt to make sure you wont lose a visitor if two hit the page at the same time and the script saves only unique IPs. 

So, here it is:

<?php$filename = "hits.Txt";$fp = fopen($filename,"r+");If(flock($fp,LOCK_EX)){   $file = file($filename);   array_push($file,getenv("REMOTE_ADDR")."and");   $file = array_unique($file);   $hits = count($file);   echo "($hits)";   $filestr = implode("",$file);   ftruncate($fp,0);   fwrite($fp,$filestr);   flock($fp, LOCK_UN);}Else{   print "(?)";}Fclose($fp);?>

-reply by Rookie

 

Share this post


Link to post
Share on other sites
"n" not "and"Php Unique Hit Counter

Replace "and" with "n" in the line:

array_push($file,getenv("REMOTE_ADDR")."n");

I don't know why this came out wrong...

-reply by rookie

 

Share this post


Link to post
Share on other sites
It should be accuratePhp Unique Hit Counter

In simple, If you are going to be using a counter on your site...It should be accurate from the first visit, Other wise what is the point of it?

-reply by MayhemOfHell

Share this post


Link to post
Share on other sites

Well it has been a long time since the last post but as I made some changes to the code, so I am posting the changes I made.

Now it only writes unique ip's to the counter.txt itself

<?php$ip = getenv("REMOTE_ADDR");$file="counter.txt";$fd = fopen ($file, "r");$fstring = fread ($fd , filesize ($file));$hits=count($file);fclose($fd);if(!strpos($fstring,$ip)){	$fd = fopen ($file, "w");	$fout= fwrite ($fd, $fstring."\n".$ip);	fclose($fd);	echo ++$hits;}else	echo $hits;?>

Share this post


Link to post
Share on other sites

hello people, I' am aware that this might be an old post, but I hope that someone will help. This script doesnt work for me. I have just uploaded the 'counter.php' and 'hits.txt' files under a directory called 'counter', in my webserver.In the file 'index.html' of my site, I have added within the <body>: <?php include("counter/counter.php"); ?>.What am I doing wrong?

Share this post


Link to post
Share on other sites

hello people, I' am aware that this might be an old post, but I hope that someone will help. This script doesnt work for me. I have just uploaded the 'counter.php' and 'hits.txt' files under a directory called 'counter', in my webserver.
In the file 'index.html' of my site, I have added within the <body>: <?php include("counter/counter.php"); ?>.
What am I doing wrong?


You cannot include a PHP code into an index.html file, that's why its not working ... to make it work, you have include that php line in an index.php file.
to do this simply save your index.html file as index.php ... & upload it to ur server

Share this post


Link to post
Share on other sites

cool, so i thought i would make an improvement so that the hits.txt file doesnt get super long and take forever to parse:

also note that i fixed the error in the code that caused a warning to display when you upload a blank hits.txt text file and run the code for the first time.

i also solved the issue some people were having (me included) of having duplicate hits register if you refresh the page.

hope you enjoy the update!

p.s. if it's your first time loading the page, then it displays a welcome message (you may want to remove this)

<?php/* be sure to change the permissions of hits.txt to 777   using either chmod or filezilla*/$filename = "hits.txt";$fileIn = file($filename);$file = array_unique($fileIn); //convert to an array with only unique items in it$hits = count($file); //count how many unique lines are in the fileecho $hits;$fd = fopen ($filename , "r"); //open file to readif ($hits > 0){  $fstring = fread ($fd , filesize ($filename)); //read in as string}fclose($fd);$myAddr = getenv("REMOTE_ADDR"); //get the ip address of client/*check to see if they've already logged an entry, if so,  then don't log another one in order to save on the file size*/$pos = strpos($fstring, $myAddr);if ($pos !== false) {}else{  $fd = fopen ($filename , "w");  $fcounted = $fstring."\n".$myAddr;  $fout= fwrite ($fd , $fcounted );  fclose($fd);  echo " welcome, new visitor!"; }?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.