Her

Home Web Programming PHP Users Online

Users Online

Author: ViButX Author's URL: www.dannyscripts.com More by this author

Users OnlineFirst Run this SQL In your MYSQL database..

CREATE TABLE Totalonline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);

Next we will have to put this code on our page to show how many people are online! Its pretty easy.

<?php
//fill in your database,name,pw.
$server  =  "localhost" ;
$db_user  =  "username" ;
$db_pass  =  "password" ;
$database  =  "users" ;
$timeoutseconds  =  300 ;

//get the time
$timestamp  =  time ();
$timeout  =  $timestamp - $timeoutseconds ;

//connect to database
mysql_connect ( $server ,  $db_user ,  $db_pass );

//insert the values
$insert  =  mysql_db_query ( $database ,  "INSERT INTO totalonline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')" );
if(!( $insert )) {
print  "totalonline Insert Failed > " ;
}

//delete values when they leave
$delete  =  mysql_db_query ( $database ,  "DELETE FROM totalonline WHERE timestamp<$timeout" );
if(!( $delete )) {
print  "totalonline Delete Failed > " ;
}

//grab the results
$result  =  mysql_db_query ( $database ,  "SELECT DISTINCT ip FROM totalonline WHERE file='$PHP_SELF'" );
if(!( $result )) {
print  "totalonline Select Error > " ;
}

//number of rows = the number of people online
$user  =  mysql_num_rows ( $result );

//spit out the results
mysql_close ();
if( $user  ==  1 ) {
print( "$user User onlinen" );
} else {
print( "$user Users onlinen" );
}
?>