Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Php Unique Hit Counter


54 replies to this topic

#51 Guest_Manav_*

  • Guests

Posted 28 October 2010 - 12:45 PM

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;
?>


#52 Guest_nico_*

  • Guests

Posted 08 February 2011 - 08:45 PM

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?

#53 Guest_maciej_*

  • Guests

Posted 01 May 2011 - 07:01 PM

any idea why my counter is resetting after a couple of days?

#54 Guest_Mamdoo_*

  • Guests

Posted 24 June 2011 - 12:43 PM

View Postnico, on 08 February 2011 - 08:45 PM, said:

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

#55 Guest_guest_*

  • Guests

Posted 17 March 2012 - 08:41 PM

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 file
echo $hits;
$fd = fopen ($filename , "r"); //open file to read
if ($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!"; 
}
?>





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users