Online Users

April 13th, 2009 | Tags:

Tutorial Description:

Learn to show how many users browsing your website.

1. Begin with a blank page and save it as usersonline.txt
2. Create another page and put this code in it.

<?php

// Store user IP address.
$remote = $_SERVER["REMOTE_ADDR"];

// File name.
$file = "usersonline.txt";

//How long the user stays on, before classed offline [in seconds]
$timeoutseconds = 600;

// Current time.
$timestamp = time();
$timeout = ($timestamp-$timeoutseconds);

// Open file.
$fp = fopen("$file", "a+");
$write = $remote ."||". $timestamp;

// Save new data in file.
fwrite($fp, $write);

// Close file.
fclose($fp);

// See how much users online.
$online_array = array();
$file_array = file($file);
foreach(
$file_array as $newdata)
{
list(
$ip, $time) = explode("||", $newdata);
if(
$time >= $timeout)
{
array_push($online_array, $ip);
}
}
$online_array = array_unique($online_array);
$online = count($online_array);

echo

'Users Online:' .$online;
?>

3. Name this file usersonline.php
4. Put this code where you want the users online to show up.
<?php
include("usersonline.php");
?>

No comments yet.
TOP