The following (free) PHP script shows the total number of unique visitors that are currently online on your website, based on their IP address.
The first step is to create a file called visitors.db and to change its permissions to 666 via FTP (e.g. with the use of the FTP client FileZilla). This file will be our little database file to store the actual number of visitors. The permission 666 means that all users can read and write to that file, so our script can read the current number of visitors and update the file periodically.
Once this file is created, create a blank file called visitors.php and paste the following code into it:
<?php /************************************************************************* php easy :: online visitors counter scripts set - PHP Include Version ========================================================================== Author: php easy code, www.phpeasycode.com Web Site: http://www.phpeasycode.com Contact: webmaster @ phpeasycode.com *************************************************************************/ $dbfile = "visitors.db"; // path to data file $expire = 300; // average time in seconds to consider someone online before removing from the list if(!file_exists($dbfile)) { die("Error: Data file " . $dbfile . " NOT FOUND!"); } if(!is_writable($dbfile)) { die("Error: Data file " . $dbfile . " is NOT writable! Please CHMOD it to 666!"); } function CountVisitors() { global $dbfile, $expire; $cur_ip = getIP(); $cur_time = time(); $dbary_new = array(); $dbary = unserialize(file_get_contents($dbfile)); if(is_array($dbary)) { while(list($user_ip, $user_time) = each($dbary)) { if(($user_ip != $cur_ip) && (($user_time + $expire) > $cur_time)) { $dbary_new[$user_ip] = $user_time; } } } $dbary_new[$cur_ip] = $cur_time; // add record for current user $fp = fopen($dbfile, "w"); fputs($fp, serialize($dbary_new)); fclose($fp); $out = sprintf("%03d", count($dbary_new)); // format the result to display 3 digits with leading 0's return $out; } function getIP() { if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; elseif(isset($_SERVER['REMOTE_ADDR'])) $ip = $_SERVER['REMOTE_ADDR']; else $ip = "0"; return $ip; } $visitors_online = CountVisitors(); ?> <p>Visitors online: <b><?=$visitors_online;?></b></p>
This PHP file will check the IP address used by each of your site’s visitors and, based on this IP address, edit the visitors.db file with that number. Highlighted in the PHP code snippet above is line number 11. You can change this number to a higher or lower value if you wish.
Now the last step is to include this PHP file into all of your webpages that you want this number to show on. To do this, we use the include statement.
<?php include("visitors.php");?>
For example, you can include the total online visitor count in the footer of each webpage on your website as follows:
<html> <head> <title>Visitor counter script</title> </head> <body> <p>Some body text goes here</p> <footer> <?php include("visitors.php");?> </footer> </body> </html>
If everything went fine, you should see the output: Visitors online: 001.
Hi,
Is there a way to show:
total visitors for the entire site,
total current visitor on site,
total page hit for current page.
eg: (main site page)
Visitors online: 210
Total visitors: 8263 from 01-Feb-2010
This page hit: 8263
eg:
Visitors online: 210
This page hit: 563
Thanks.
Thank you very match, Good Information.
Thanks very much.
Please can you give a complete tutorial on how to comment on a post with php and mysql? Will appreciate that. Thanks for the post.
Thanks, I used this code but the visitors.db file data clear once reached “Visitors online: 1050 ”
how to solve this issue…
please give me visitors.db file.
create blank file with th name visitors.db.
and read everything carefully
Doesn’t work for me. Only shows 001, even though I have many other live visitors on the website according to Google analytics. I’ve given all the right permissions to the db file and I’ve increased the seconds to keep as live visitor. Still nothing.
abey visitor.db ki file to dede
Does not work with laravel
I have a variable called $login, with represents the emailadresses of the users.
How do I include that in the array $dbary_new?
I tried everything, but I don’t really understand arrays.
Please help
I have a var called $login which contains the emailadres of the user.
Is there a way to integrate $login in the array $dbary_new
Please help…
Thanks.
Aad