Table of contents:
- The concept
- Creating the database
- The code
- Implementing it
Requirements:
- Server with PHP/MySQL capabilities.
- Basic PHP/MySQL knowledge.
1. The concept
The concept of this is fairly simple. We will generate a UNIX timestamp with the time() command and add a record to a SQL database of that specific IP address along with the timestamp. Everytime a page is loaded, we will delete the records which have expired. We will use an expiration time of 10 minutes (600 seconds). You will be able to change this by changing the $expiration_time variable. To compare the timestamp with the current time, we will perform a simple math subtraction operation and then do an if statement.
2. The database
As only one table is needed for this to work, you can just use an existing database. Go to phpMyAdmin or any other SQL database administration tool. Create a table called onlinerecords with 2 fields and submit the form. Name the first field ip. Set the type to VARCHAR and the maximum length to 16 characters. Set the primary key attribute to it. Name the second field time. Set the type to INT. Submit the form.
If you failed to do this, or cannot do it for some reason, just use this code in a SQL query.
CREATE TABLE `onlinerecords` (
`ip` VARCHAR( 16 ) NOT NULL ,
`time` INT NOT NULL ,
PRIMARY KEY ( `ip` )
) ENGINE = MYISAM 3. The code
<?php
//Change this if you want the expiration time to change.
$expiration_time = 600;
//Change the login variables if needed. (server, username, password)
@mysql_connect('localhost', 'root', '') OR die('MySQL error - failed to connect.');
//Change the database if needed.
@mysql_select_db('online') OR die('MySQL error - failed to select the database.');
$minimum_time = time() - 600;
$current_time = time();
$ip = $_SERVER["REMOTE_ADDR"];
//Delete the records that have already expired.
//Unbuffered query means that we will not use the results.
@mysql_unbuffered_query("DELETE FROM `onlinerecords` WHERE `time` < '{$minimum_time}'") OR die('MySQL error');
//Now let's search for the users IP. If it is there, update just the time. If not, add it.
$ip_check = @mysql_query("SELECT * FROM `onlinerecords` WHERE `ip` = '{$ip}'") OR die('MySQL error');
if (mysql_num_rows($ip_check) == 1) { //IP exists, let's just update the time.
@mysql_unbuffered_query("UPDATE `onlinerecords` SET `time` = '{$current_time}' WHERE `ip` = '{$ip}'") OR die('MySQL error');
}
else { //IP doesn't exist, let's add it.
@mysql_unbuffered_query("INSERT INTO `onlinerecords` (`ip`,`time`) VALUES ('{$ip}', '{$current_time}')") OR die('MySQL error');
}
//All that's left to do is get the number of active users.
//As we've already deleted the inactive ones, let's just count the number of rows.
$count_query = @mysql_query("SELECT COUNT(*) FROM `onlinerecords`") OR die('MySQL error');
$active_users = @mysql_result($count_query, 0);
@mysql_free_result($count_query);
@mysql_close();
echo "Active users: {$active_users}";
?>
That's it. Simple as that.4. Implementing it.
Make sure this is loaded on every page. You do not have to echo out the number of the active users every time. Just delete the last line which echoes out the text and make the script an include at the top of your page (or just add it to the top - but better to make it an include, more efficient) and then just use the $active_users variable where needed.
If you need at any point with this tutorial or can't get it to work, feel free to email me at as@ingmaras.com, good luck!
discuss this topic to forum
