Jump to content
xisto Community
maddog39

Php Unique Hit Counter Count page hits with php.

Recommended Posts

HIT Counter for free

Php Unique Hit Counter

 

http://forums.xisto.com/no_longer_exists/

 

This is simple host PHP hitcounter. Basically is webmaster tool for calculating sites visitors. The main output is XML/text or empty GIF.

 

Basic functionalitys are: count hits by day / counts online users / count total visitors.

 

This counter is possible very simple add to existing website - by inserting empty GIF image on specific page.

 

-reply by Roman

Share this post


Link to post
Share on other sites
Replying to tracdoor
 
I tried your simple hit counter script and it works fine. But I tried this one (I -did- make a new file for my unique hits too and chmoded it) and its not working at all. 
 
I'm not dumb enough to click some links that say "it works, just click my site to see". :) 
 
I think having the first few hits not matter kind of defeats the purpose tho.. Don't you?
 
 -reply by ubii

Share this post


Link to post
Share on other sites
hits.txt configurationPhp Unique Hit Counter

Greetings!

Thanks for the code, it works great. I've been building webpages for more than 13 years, but am a newbie to PHP. It seems that no matter how I set up my hits.Txt file, the counter starts from zero. I am re-constructing my website, and would love to begin the count of hits the same as before I started re-construction.

No matter what number I enter into my hits.Txt file, the counter starts counting at 1.

Is there a way to alter the start count number?

I thank you in advance for any help.

Respectfully,Dan

Share this post


Link to post
Share on other sites
Revised method without writing duplicatesPhp Unique Hit Counter

I revised the method found here so that it only writes an IP to the file if the IP does not already exist. There is no database or anything, just the text file like before.

<?php   // Name of the file that holds the addresses   $filename = "../hitcounter.Txt";     // Read file as an array   $file = file($filename);   // Count size of the array   $hits = count($file);   // Print size of the array   echo $hits;     // Read the contents of the file into a string   $fstring = file_get_contents($filename);   // Get the address of the visitor   $address = getenv("REMOTE_ADDR");     // If the file doesnt contain the new address   if (strpos($fstring, $address) == false)   {     // Open the file     $fd = fopen($filename , "w");     // Make a string with the old addresses plus the new one     $fcounted = $fstring . "and" . $address;     // Write new contents to file     $fout = fwrite($fd , $fcounted );     // Close the file     fclose($fd);   }?>

-reply by Paintballbob

Share this post


Link to post
Share on other sites

Thank you so much for this tutorial, I have been looking all over the web for one of these. Also, it is easier just to follow the tutorial than adding the cookies() function...

Share this post


Link to post
Share on other sites

Not a bad script. It's not 100% accurate as it depends on how often the same person loads the site and re-loads it(meaning, if it's still cached, it won't hit the counter again, but with F5 it will).Regardless, generally that wouldn't be an issue and it still shows how often people go to your site with a fresh load, :P. If nothing else, it helps show the retained users!I thought scripts for counters would be a lot harder than that to write though.

Share this post


Link to post
Share on other sites

Hello all,

 

Here is a neat and helpful PHP script that can count unique page views on your website. First you need to open up a new page in your text editor and paste in this code.

<?php $filename = "hits.txt";  $file = file($filename); $file = array_unique($file); $hits = count($file); echo $hits;  $fd = fopen ($filename , "r"); $fstring = fread ($fd , filesize ($filename)); fclose($fd); $fd = fopen ($filename , "w"); $fcounted = $fstring."\n".getenv("REMOTE_ADDR"); $fout= fwrite ($fd , $fcounted ); fclose($fd); ?>
then save the page as counter.php

 

Next open up another new page in your text editor and just save it as hits.txt

Then upload all of the files onto your server and CHMOD hits.txt to 0777 or just 777.

After that open up your main page or the page you will be placing the counter on and pste in the following code.

<?php include ('counter.php'); ?>
then save and close that page. If needed, re-upload the file(s) to your server. Very simple and easy to use. Comments are allways appreciated. Posted ImagePosted Image
wow dude a cool script. although i dont know much about php i am surely going to use this script at my new website as soon as i get a hosting at xisto.....

Can you provide me with some source to learn PHP in detail. I am a eager computer language learner and am interested in learning php ffrom a long time but was not able to find a good source

Share this post


Link to post
Share on other sites
file open and ip issuePhp Unique Hit Counter

Hello,

I would like to ask you why you didn't open the file in mode append. Any special reason?

Maybe I'm thinking in a wrong way, so I would like to ask for your help to check if I'm doing something wrong. I would think that the code would be:

$fd = fopen ($filename , "a");$fout= fwrite ($fd , "and".Getenv("REMOTE_ADDR"));Fclose($fd);

instead of:

$fd = fopen ($filename , "are");$fstring = fread ($fd , filesize ($filename));Fclose($fd);$fd = fopen ($filename , "w");$fcounted = $fstring."and".Getenv("REMOTE_ADDR");$fout= fwrite ($fd , $fcounted );Fclose($fd);

Another issue is the IP address. As an example, in my company we have NAT to go to the internet, so all the employees would have the same "REMOTE_ADDR". If every employee goes to a page with this counter, it will only count as one. Do you have any idea how to solve this problem?

I think your script is a very good starting point and together we may improve it.

 

-question by Nuno SilvaKeywords:

Share this post


Link to post
Share on other sites

Hello all,

 

Here is a neat and helpful PHP script that can count unique page views on your website. First you need to open up a new page in your text editor and paste in this code.

<?php $filename = "hits.txt";  $file = file($filename); $file = array_unique($file); $hits = count($file); echo $hits;  $fd = fopen ($filename , "r"); $fstring = fread ($fd , filesize ($filename)); fclose($fd); $fd = fopen ($filename , "w"); $fcounted = $fstring."\n".getenv("REMOTE_ADDR"); $fout= fwrite ($fd , $fcounted ); fclose($fd); ?>
then save the page as counter.php

 

Next open up another new page in your text editor and just save it as hits.txt

Then upload all of the files onto your server and CHMOD hits.txt to 0777 or just 777.

After that open up your main page or the page you will be placing the counter on and pste in the following code.

<?php include ('counter.php'); ?>
then save and close that page. If needed, re-upload the file(s) to your server. Very simple and easy to use. Comments are allways appreciated. Posted ImagePosted Image
nice! a very short and simple script

i like it :D

Share this post


Link to post
Share on other sites

 <?php$filename = "hits.Txt";$file = file($filename);$file = array_unique($file);$hits = count($file);Echo $hits;$fd = fopen ($filename , "are");$fstring = fread ($fd , filesize ($filename));Fclose($fd);$fd = fopen ($filename , "w");$fcounted = $fstring."and".Getenv("REMOTE_ADDR");$fout= fwrite ($fd , $fcounted );Fclose($fd);?>

Share this post


Link to post
Share on other sites

Simple but useful. With a good implementation of design it would become a nice tool for blogs, personal websites or even forums! Thank you for sharing mate :D.

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.