Show current total of nline visitors with PHP

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 easy :: online visitors counter scripts set - PHP Include Version
Author: php easy code,
Web Site:
Contact: webmaster @

$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));

$out = sprintf("%03d", count($dbary_new)); // format the result to display 3 digits with leading 0's
return $out;

function getIP() {
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:

<title>Visitor counter script</title>
<p>Some body text goes here</p>

<?php include("visitors.php");?>


If everything went fine, you should see the output: Visitors online: 001.


Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.


11 thoughts on “Show current total of nline visitors with PHP

  1. 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

    Visitors online: 210
    This page hit: 563


  2. 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.

  3. 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.

  4. 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

  5. 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…


Leave a Reply

Your email address will not be published. Required fields are marked *