Version: 1.0

Type: Full Script

Category: HTTP

License: GNU General Public License

Description: This script counts the number of users currently viewing that page, or site. Uses PHP4 Sessions and MySQL.



<?php
/******

Online User Counter. 1.0

This script was created by Andreas Hillberg (andreas@hillberg.nu)
Please tell me that you are using my script, so I'll find a reason to release
other scripts I've made.

This script is a beta just yet, I am not 100% certain it will work in all curcumstainces.
Feel free to alter the code as you see fit, but if you do I would be glad that you
send me the altered code. So I'll learn more of this.

What it does is that it registers a PHP session with the ID of session and a time stamp.
It registers it to a MySQL database and later pulls out all entries and counts the
number of ID's that was registered 15 minutes ago.
You view the page again, and the MySQL will get updated.
Entries registered before 15 minutes ago will get deleted so it won't overload your database.

SET UP MYSQL LIKE THIS!!

create table YOURTABLE (
SID varchar(100) NOT NULL,
time varchar(15) NOT NULL,
day varchar (3) NOT NULL);

Config your settings below
*****/
$Session_name = "Your_session"; 	// The Sessions name, write "default" for default name.
$host = "localhost";						// Your host
$username = "username"; 				// Your MySQL username
$password = "password"; 			// Your MySQL password
$database = "database"; 			// Your Database of choice
$table = "table";						// Your Table of choice, ex. "online_users"

/***
Don't mess with the code below if you don't know what you're doing!
**/

	// Starts Session
if ($Session_name == "default") {
	session_start();
}
else {
	session_name("$Session_name");
	session_start("$Session_name");
}

$SID = session_id();
$time = time();
$dag = date("z");
$nu = time()-900;	

	
	//This connects to the MySQL server
mysql_connect ($host, $username, $password) OR DIE ("Could not connect to MySQL");
mysql_select_db($database) OR DIE ("Can't select database.");

	// Check to see if the session_id is already registerd
$sidcheck = mysql_query("SELECT count(*) FROM $table WHERE SID='$SID'");
$sid_check = mysql_result($sidcheck,0);

if ($sid_check == "0") {
		// If not, the session_id will be stored in MySQL
	mysql_query("INSERT INTO $table VALUES ('$SID','$time','$dag')");		
} else {		
		// If it is, it will register a new time to the session.
	mysql_query("UPDATE $table SET time='$time' WHERE SID='$SID'");				
}
	
	// This is it, this counts the users currently online	
$count_users = mysql_query("SELECT count(*) FROM $table WHERE time>$nu AND day=$dag");
$users_online = mysql_result($count_users,0);

	// This deletes old ids, so your db will not get overloaded.
	
mysql_query("DELETE FROM $table WHERE time<$nu");
mysql_query("DELETE FROM $table WHERE day != $dag");

	mysql_close();
	
	if ($users_online == "1") {
		echo "You are alone to view this page right now.\n";
		}
	else {
		echo "There's $users_online people viewing this page right now.\n";
		}
	
?>