Location: PHPKode > scripts > UsersOnline > usersonline/users-online.php
<?

//
//	users_online
//
//	hide@address.com
//	
//	a class that registers and counts the number of users on a website, using a timeout method/approach
//

/*
Copyright (C) 2002  hide@address.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

//
//	MySQL version
//		for MySQL only. Replace mysql_* with the apropriate functions to access your DBMS. Other changes may be necessary.
//


class users_online {

	var $s_host = "localhost";		//	host
	var $s_username = "username";		//	username
	var $s_password = "password";		//	password
	var $s_database = "database";		//	database name 

	var $s_db_link;

	var $n_secs = 250;		        //	seconds to timeout

	//
	//	the following applies if you want to save results in a database
	//

	var $s_stats_table = "statistics";
	var $s_time_field = "logtime";
	var $s_number_field = "logged_users";



	function users_online ( $n_ip ) {

		$this->n_ip = $n_ip;

		$this->s_db_link = mysql_connect ( $this->s_host, $this->s_username, $this->s_password );
		mysql_select_db ( $this->s_database );

	}

	//
	//	main
	//
	//		calls the necessary functions in the correct order to get the desired results
	//

	function main () {
		if ( $this->is_logged ()  ) {
			$this->update ();
		}
		else {
			$this->new_user ();
		}

		$this->clean ();
	}

	//
	//	new_user
	//
	//		inserts a user into the database
	//

	function new_user () {
		$timestamp = time ();
		$sql = "insert into users_online ( id, ip, timestamp ) values ( '', '$this->n_ip', '$timestamp' )";
		mysql_query ( $sql, $this->s_db_link );
	}

	//
	//	is_logged
	//
	//		checks if a user is already in the database.
	//

	function is_logged () {
		$sql = "select * from users_online where ip = '" . $this->n_ip . "'";
		if ( mysql_num_rows ( mysql_query ( $sql, $this->s_db_link ) ) > 0 ) {
			return 1;
		}
		else {
			return 0;
		}
	}

	//
	//	clean
	//
	//		deletes "timeouted" users from the database
	//

	function clean () {
		$n_now = time ();
		$sql = "delete from users_online where timestamp < ( $n_now - $this->n_secs )";
		mysql_query ( $sql, $this->s_db_link );
	}

	//
	//	update
	//
	//		updates timestamp value in the database for the active user
	//

	function update () {
		$timestamp = time ();
		$sql = "update users_online set timestamp = $timestamp where ip = '$this->n_ip'";
		mysql_query ( $sql, $this->s_db_link );
	}

	//
	//	count
	//
	//		returns the number of users in the database at runtime
	//

	function count () {
		$sql = "select * from users_online";
		$count = mysql_num_rows ( mysql_query ( $sql, $this->s_db_link ) );
		return $count;
	}

	//
	//	db_log
	//
	//		logs result into a database
	//

	function db_log ( $n_users = 0 ) {

		if ( $n_users == 0 ) {
			$n_users = $this->count ();
		}

		$n_now = time();
		$sql = "insert into $this->s_stats_table values ('', '$n_now', '$n_users')";

		mysql_query ( $sql );
	}

	//
	//	db_stats
	//
	//		shows db log in an HTML table
	//

	function db_stats ( ) {
		$sql = "select * from $this->s_stats_table limit 10";
		$rs = mysql_query ( $sql );

		echo "<table border=0>";
		echo "<tr>";
		echo "<td>Date</td>";
		echo "<td>User Count</td>";
		echo "</tr>";
		while ( $rec = mysql_fetch_array ( $rs ) ) {
			echo "<tr>";		
			echo "<td>", date ( "d/m/y h:i:s", $rec[$this->s_time_field] ), "<td>";
echo "<td>", $rec[$this->s_number_field], "<td>";
			echo "</tr>";
		}
		echo "</table>";
	}

}

?>
Return current item: UsersOnline