Jump to content
xisto Community
Sign in to follow this  
FaLgoR

Visitors Counter With PHP (no MySQL needed)

Recommended Posts

Well, here we will make an hit counterusing php and an *.txt file. This script is so simple, but very usefull. First, le'ts make an file called counter.php, with the following codes inside:

 

<?$file = 'counter.txt'; // this is the file where the number of visitors will be written$fopen = fopen("$file", "r"); // open the text file$count = fread($fopen, 1024); // read the text filefclose($fp); // close the text file$count = $count + 1; // add one more hitecho "$count"; // print the number$fopen = fopen("counter.txt", "w"); // open the file againfwrite($fp, $count); // write the new numberfclose($fp); // close the file?> 

That's all, it must e working fine (I'm using it :]). To show the number of hits on your site and add the counter, just insert this line where the counter will be:

<? include("counter.php"); ?>
It will show the visitors AND add the counter.

 

Problems, just post ;D

 

----------------------------- Edited -------------------------------------

I forgot, youll have to make an blank file called counter.txt. Have fun =P


Share this post


Link to post
Share on other sites

I also used a script like this for counting but I found that it wasn't really enough information. It would be more accurate to count per session using cookies or something. I did a counter with cookies but then I wanted even more information like referrer, time spent, etc. I kind of gave up on this and just joined statcounter.com. They do it for you and you don't have to waste time making the program. the only problem with statcounter is that it only shows the last 100 page loads for free accounts. But, I find the information very useful.O, thanks for putting up the code. It actually is a good counter if you aren't worried about analyzing traffic. Many people will undoubtedly find it useful.

Share this post


Link to post
Share on other sites

HOW ABOUT THIS?------------------TO INCREMENT HITS BY 1------------------<?php $filename= "hits.txt" ; $fd = fopen ($filename , "r") or die ("PHP script cant open $filename") ; $fstring = fread ($fd , filesize ($filename)) ; echo "$fstring" ; fclose($fd) ; $fd = fopen ($filename , "w") or die ("PHP script cant open $filename") ; $fcounted = $fstring + 1 ; $fout= fwrite ($fd , $fcounted ) ; fclose($fd) ; ?> OR<?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); ?> HOPE THIS HELPS.

Share this post


Link to post
Share on other sites

I also used a script like this for counting but I found that it wasn't really enough information.  It would be more accurate to count per session using cookies or something.  I did a counter with cookies but then I wanted even more information like referrer, time spent, etc.  I kind of gave up on this and just joined statcounter.com.  They do it for you and you don't have to waste time making the program. 

 

the only problem with statcounter is that it only shows the last 100 page loads for free accounts.  But, I find the information very useful.

 

O, thanks for putting up the code. It actually is a good counter if you aren't worried about analyzing traffic.  Many people will undoubtedly find it useful.

51325[/snapback]


I am not showing an page tracker, this is just a hit counter! =/

Share this post


Link to post
Share on other sites

Nice CodeYou can Make DB (FlatFile) on your host with 755 PM Access For Rewrite onand then make on php page and when user view this page the page input the 1 to file for example if in file is 1the php page get File data and add one to it and rewrite to filethats allu can find many CMs for this Post in google or hotscripts or Script Websites

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
Sign in to follow this  

×
×
  • 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.