Jump to content
xisto Community
maddog39

Php Unique Hit Counter Count page hits with php.

Recommended Posts

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.

<?phpinclude ('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. :D:D

Share this post


Link to post
Share on other sites

Very nice! All of the other ones I've seen are very complex and I have no idea what they mean, but I understand this one. Do you mind if I use it on my site?

Share this post


Link to post
Share on other sites

It counts unique. The first couple hits are counted as page views till it kicks in. try it on my site at the bottum of the page. http://forums.xisto.com/no_longer_exists/ :angry:

58225[/snapback]

Well, here's what happened.

 

I visited your website. After that, I saw the counter. Then I reloaded the page. It stays the same. Then I reloaded it again, then the counter increased by +1. After that, I reloaded and reloaded the page again. It stays the same. Then I went to http://forums.xisto.com/no_longer_exists/ and I saw many logs of my ip address.

 

You must use cookies() in order for it to work well. Or sessions.

Share this post


Link to post
Share on other sites

In my book thats not needed and it makes things complicated. It can mis count thats true. However its sensitive in different ways. So what you said depends. :angry:

Share this post


Link to post
Share on other sites

In my book thats not needed and it makes things complicated. It can mis count thats true. However its sensitive in different ways. So what you said depends. :angry:

58596[/snapback]

Ok, maybe i'll try to modify your code and improve it. Don't worry, i'll put your copyright. Can you please add some comments and explaination to your code and add it again here, in your thread?

Share this post


Link to post
Share on other sites

Okaay here si the code with added comments. Here is counter.php.

<?php

//The file where IPs are logged.

$filename = "hits.txt";

 

//Counting the hits from hits.txt.

$file = file($filename);

$file = array_unique($file);

$hits = count($file);

echo $hits;

 

//Writing the IPs to the file hits.txt

$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);

?>

I think that should clear some things up. Also I will add a cookie system since that would probably be the best for this since cookies are not deleted and easier to do than sessions. :angry:

Share this post


Link to post
Share on other sites

Okaay here si the code with added comments. Here is counter.php.

<?php

//The file where IPs are logged.

$filename = "hits.txt";

 

//Counting the hits from hits.txt.

$file = file($filename);

$file = array_unique($file);

$hits = count($file);

echo $hits;

 

//Writing the IPs to the file hits.txt

$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);

?>

I think that should clear some things up. Also I will add a cookie system since that would probably be the best for this since cookies are not deleted and easier to do than sessions. :angry:

[snapback]59073[/snapback]

Please, more expaination... while reading your script, i learned a new function, and i didn't know that it exists, the array_unique() function. Please explain all of what's happening.

Share this post


Link to post
Share on other sites

Okay, first it looks at the filename and gets that. Then it counts all of the logged hits in hits.txt and displays it. After that it counts the new hits from the person coming in and writes them to hits.txt to be logged. Does that clear it up anymore? :angry:

Share this post


Link to post
Share on other sites

Okay, first it looks at the filename and gets that. Then it counts all of the logged hits in hits.txt and displays it. After that it counts the new hits from the person coming in and writes them to hits.txt to be logged. Does that clear it up anymore? :angry:

59270[/snapback]

More explaination... Especially, the section where it finds the ip address, and if it's found, then what will really happens after that?

 

Anyways, check out my new website http://www.karlo.ph.tc/

 

Oh yeah, do you know any free providers that supports the changing of name servers? like the one in uni.cc like a dns service...

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.