This tutorial will tells you to create a user online detection system. This tutorial has required a mysql database and php enable server.
Please follow these steps to complete your own UserOnline System.
Create table “user_online” in mysql in database “database1″.
CREATE TABLE `user_online` (
`userip` char(100) NOT NULL default ”,
`time` int(11) NOT NULL default ‘0’
) TYPE=MyISAM;
Create a file name user_online.php.
Write three variables for userip, current time and timeout. I have write the following variables:
$userip=$_SERVER[“REMOTE_ADDR”];
$to_secs = 120; // time to reset IP address’s value in seconds, default here is 120 (2 minutes)
$t_stamp = time();
$timeout = $t_stamp – $to_secs;
Now is the time to connect this script with a database server and execute some queries. I use mysql server for this system.
$server = “localhost”; // Your mySQL Server, most cases “localhost”
$db_user = “root”; // Your mySQL Username
$db_pass = “password”; // Your mySQL Password
$db = “database1”; // Database Name
This command will make a connection with the database server.
mysql_connect($server, $db_user, $db_pass) or die (“Useronline Database CONNECT Error”);
This query will make an entry with the user visiting timestamp in the user_online table.
mysql_db_query($db, “INSERT INTO user_online VALUES (‘$t_stamp’,’$REMOTE_ADDR’)”) or die(“Database INSERT Error”);
This query will delete those entry which are older than 2 minutes from user_online table.
mysql_db_query($db, “DELETE FROM user_online WHERE time<$timeout”) or die(“Database DELETE Error”);
Finally these quiry will get how many users visiting your website.
$result = mysql_db_query($db, “SELECT DISTINCT userip FROM user_online”) or die(“Database SELECT Error”);
$user = mysql_num_rows($result);
mysql_close();
if ($user == 1){
echo “<b>$user</b> User Online”;
}else{
echo “<b>$user</b> Users Online”;
}
How to create a link within a PHP Page (.php, .php3, .php4)
<?php
include(“user_online.php”);
?>