Page 1 of 1

Who's online

Posted: Tue May 14, 2002 11:07 pm
by DSM
Ok, here's the project.
Portal for my daughter's 2nd grade class. Working on a "Who's Online", using cookies, with user id ($uid) and user name ($user_name).

Question is: how do I collect the uid's & user names from the cookies & put them in an array to loop thru so it will show everyone that is online?

Thx in advance.
Mike

cookies...

Posted: Tue May 14, 2002 11:32 pm
by pHaZed
Hey,
I wrote a script that does exact same thing, But doesnt use cookies it use sql...

Code: Select all

<?PHP                                                                                 
/*
Author: pHaZed
URL: www.net-espionage.com
Script: Useronline
*/
$host= "127.0.0.1";  	// MySQL Server,                  
$db_user= "user"; 	// MySQL Username                                        
$db_pass= "pass";		// MySQL Password                                        
$database= "data";	// Database Name                                              
$timeoutseconds 	= 300;			// Timeout value in seconds                                                                                                              
$timestamp=time();                                                                                            
$timeout=$timestamp-$timeoutseconds;  
mysql_connect($host, $db_user, $db_pass) or die (mysql_error());                                                                    
mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die(mysql_error()); 
mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die(mysql_error());
$result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die(mysql_error());
$user  =mysql_num_rows($result);                                                                    
mysql_close();                                                                                                
if ($user==1) 
&#123;
	echo"<font face=verdana size=1 color=#FFFFFF><B>There is $user User online</b></font>"; //single
&#125; 
else 
&#123;
	echo"<font face=verdana size=1 color=#FFFFFF><B>There are $user Users online</b></font>"; //plural
&#125;
?>
But if you dont have MySQL access, Then I cant help. Not with cookies anyway :?

Posted: Tue May 14, 2002 11:36 pm
by jason
DSM: Yeah, take a look on PHPClasses.org for User Online classes, or use the above script. They are out there in bulk...maybe a google search will turn up something.

Posted: Wed May 15, 2002 7:31 am
by DSM
pHaZed:Thx, but I have one of those already, I am looking to be able to actually list the names of who is online.

Posted: Wed May 15, 2002 8:05 am
by enygma
you'd be better off checking for the cookie and adding that name to a text file or a database with a timestamp, then defining a "timeout" for how long ago you want to show.

Since HTTP is stateless, you can't tell when people leave the site - so you just have to make a guess at an average time.

-enygma

Posted: Wed May 15, 2002 10:12 am
by DSM
Thanx guys, took enygma's advice, worked like a champ.

Posted: Wed May 15, 2002 11:24 am
by EvilWalrus
phazed: also be careful what you take credit for... if you're confused, I'd go look at my scripts on evilwalrus.com and compare the user online one to yours..