<?
class users_online {
var $s_host = DATABASE_HOST; // host
var $s_username = DATABASE_USER; // username
var $s_password = DATABASE_PASSWORD; // password
var $s_database = DATABASE_NAME; // database name
var $s_db_link;
var $n_secs = 86400; // seconds to timeout now set to 24 hours because old logins will be deleted
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 () {
$this->ipclean ();
if ( $this->is_logged () ) {
$this->clean ();
}
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 ) and ip = '$this->n_ip' ";
mysql_query ( $sql );
}
function ipclean () {
$n_now = time ();
$sql = "delete from users_online where ip = '$this->n_ip' ";
mysql_query ( $sql );
}
//
// 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 );
}
//
// 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;
}
}
?> |