What's new

Closed Show current total of online visitors with PHP

Status
Not open for further replies.

PHC-Rafael

Ice 4 sale
Contributor
Joined
Mar 19, 2017
Posts
20,355
Solutions
11,198
Reaction
23,163
Points
16,568
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:
visitors.php
PHP:
<?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.

Including the visitor count
PHP:
<?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

Live visitors in website footer
HTML:
<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.
 
Status
Not open for further replies.
Back
Top