|
|
Php Unique Hit Counter - Count page hits with php. | ||
Discussion by maddog39 with 54 Replies.
Last Update: March 17, 2012, 1:41 pm (View Latest) | Page 1 of 2 pages. | ||
![]() |
|
|
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.
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.
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.
QUOTE (maddog39)
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://gamerspub.trap17.comWell, 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://gamerspub.trap17.com/hits.txt and I saw many logs of my ip address.
You must use cookies() in order for it to work well. Or sessions.
QUOTE (maddog39)
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.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?
CODE
<?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.
QUOTE (maddog39)
Okaay here si the code with added comments. Here is counter.php.CODE
<?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.
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.
QUOTE (maddog39)
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?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...
QUOTE (maddog39)
For dynamic DNS which uses the IP address in your cPanel the only ones I know of are http://www.cjb.net and http://yi.org they are both really short DNS, FREE dns domains.Hello. I think yi.org and cjb.net dosen't support name servers. I only know uni.cc and 1dni.com do you know any other?
QUOTE (maddog39)
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.
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.
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.
I just analysed this tutorial and it works, u could have added more choices for the php to read because it might get confused on what to do, remember it needs the right thing to do or it will not work, lol, and the way u designed it is for babies, you used very old php, and the syntax is a bit corrupted and rusty but it can be fixed with a little bit of elbow greese
umm.. karlo it doesnt really matter. Just login to your cPanel and where it says account stats or something, there should be an IP address other than yours. Put that IP in there and point your URL on the dynamic DNS in addon domains in cPanel also. An example is my site. http://gamerspub.cjb.net
That should help you out some.
Php Unique Hit Counter
I put it in and do what it says, but there's no counter or anything like that. Even when I load up hits.Txt it's still blank.
-question by G-Money
-Save and upload all files to the same directory
-make sure the page that has the counter is ending with the .php extension because that is hte only way to get it running.
If your looking for more ways to set up a counter check out pixel2life.com or check out this unique hit counter script. As it is very customizable to fit with your site and works the same way this script does with flat files and no DB's.
Php Unique Hit Counter
This script is not working for more than 4 times after that echo remains
Same check it
-reply by kar32
I spent hours trying to figure out what was wrong.
*whoop*whoop* for trap17
....VERY helpful!
Now I can, When my website is functional keep track of how many ppls who have visited my page / downloaded my program..
THANKS a LOT
//Datanizze
Php Unique Hit Counter
Hi.. Easy installation and very straightforward.. However, is there any mod that can be done so that the hits.Txt file does not get too big. Thanks in advance... John
-reply by john
Similar Topics:
Php Hit Counter
Free Site Counter Stats
I Want A Good Hit Counter
Shoutbox, Made Easy! Again! with another version! (2)
|
(20) Php Emailer/contact System An email or contact system for your site
|
Loading...
HOME 





PHP Tutorials: Non-unique Hit Counter (Part 1/2)
PHP Tutorials: Unique Hit Counter (Part 2/2)
PHP Tutorials: Non-unique Hit Counter (Part 2/2)
How To Create a Hit Counter - Page Views (PHP) Part 1 of 2
Create a simple hit counter using PHP and MySQL Part 1
how to get a visitor counter for free to your website
PHP MySQL Tutorial: Create a Simple View Counter -HD-
Create a Hit Counter for your Website & Status Bar
This is HOW I GET THOUSANDS of FREE TRAFFIC visitors to my websites. How to Unique page views.
Google I/O 2009 - App Engine: Scalability, Fault Tolerance..

