bookmark - Easy Visitor Counter Easy to use and to install

Easy Visitor Counter - Easy to use and to install

 
 Discussion by jsuthers with 5 Replies.
 Last Update: May 16, 2011, 12:08 pm
 
bookmark - Easy Visitor Counter Easy to use and to install  
    
free web hosting
 
Add this to the page you want to count the visitors on:

CODE

<?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 www.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:

CODE

<?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>.';
?>

Sat May 24, 2008    Reply    New Discussion   


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:

CODE

<?php
//Update
mysql_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());

//Display
echo '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

Sun May 25, 2008    Reply    New Discussion   

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.

Sun May 25, 2008    Reply    New Discussion   

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 ( ... )
....

Sun Jun 1, 2008    Reply    New Discussion   


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

Fri Feb 19, 2010    Reply    New Discussion   

to VandanaEasy Visitor CounterUse $_SERVER 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-reply by Manoj Updhyay

 


Mon May 16, 2011    Reply    New Discussion   

Quickly Post to Easy Visitor Counter Easy to use and to install w/o signup Share Info about Easy Visitor Counter Easy to use and to install using Facebook, Twitter etc. email your friend about Easy Visitor Counter Easy to use and to install Print
Reply / Comment Ask a Question? Share / Bookmark E-Mail a Friend Print

Love Calculator How to make a love calculator for a mobile site  Love Calculator How to make a love calculator for a mobile site (1) (10) Dynamic Php Image And Better Php Code Question   Dynamic Php Image And Better Php Code Question