Honesty Rocks! truth rules.

Easy Visitor Counter Easy to use and to install

HOME      >>       Programming

jsuthers

Add this to the page you want to count the visitors on:

<?php$logfile="counter.txt";$returnpage = htmlentities($_GET['count']);if (! @$file = fopen($logfile,"r+")){$count="1";}else{$count = @fread($file, filesize($logfile)) or $count=0;fclose($file);$count++;}$file = fopen($logfile,"w+");fputs($file, $count);fclose($file);?>

The link to the page you want to count the visitors should be http://ww38.yoursite.com/yourpage.php?count
The visitors numbers will be saved in counter.txt

Add this to the page where you want to show how many visitors the page have had:
<?php$logfile="counter.txt";if (! @$file = fopen($logfile,"r+")){$count="0";}else{$count = @fread($file, filesize($logfile)) or $count=0;fclose($file);}echo 'Visitors: <font color="#FF6600">'.$count.'</font>.';?>


sparkx

That seems like a lot of work to count the number of visitors. Also how safe is it? I get a little worried when you try to edit actual files rather then using a database. This is how I would count visitors using a database:

<?php //Updatemysql_connect("localhost", "username", "password") or die(mysql_error());mysql_select_db("database") or die(mysql_error());$row = mysql_fetch_array(mysql_query("SELECT * FROM info WHERE ID='1'") or die(mysql_error())); $count = $row['count']+1;$result = mysql_query("UPDATE info SET count='$count' WHERE ID='1'") or die(mysql_error()); //Displayecho 'Visitors: <font color="#FF6600">'.$count.'</font>.';?>
This method is great and very secure unless you don't have MySQL access. Otherwise your script would be the only option and would work good as long as you made sure your server only allowed editing of that file only and only from the server itself (not an external server/hacker).

Your script is still good to know though because when you get into more advanced things you might need to actually save a file to the server (for example when someone is installing your open source code on their server).

Sparkx

FirefoxRocks

Yes I think that a database would be a better idea as it requires a password to access and is less work to use than by writing to a file each time. This is because unless you have specific permissions set, anyone can read that file and that may not be a good thing.With a database you can do so much more though, such as IP logging, browser (user-agent) identification, etc. This may or may not be useful but to most people it should be because it helps efficiently target their audience in some cases.


magiccode91405241511

But sometime that only a value then it need to be read from a db may waste it space.If the code was used on small sites would be fine.Writing to file just as to database.But instead I am thinking about the code changed a bit may be better by first initial the variables.$count = 0;$file = null;if ( ... ) ....


iGuest

How to block the internal ip address in hit count of the website?Easy Visitor CounterHow to block the internal ip address in hit count of the website?-question by vandana

iGuest

to VandanaEasy Visitor CounterUse $_SERVER['REMOTE_ADDR'] to check and compare the ip address. If you know the ip address of the so called your enemy (bloked ip ) if he traced then use header to redirect some other site<?phpIf ($$_SERVER['REMOTE_ADDR']=="127.0.0.1){header('WWW-Authenticate: Basic realm="Restricted Section"');Header('HTTP/1.0 401 Unauthorized');Die("Please enter your username and password");}?>-reply by Manoj Updhyay