Who's online

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Who's online

Post 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
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

cookies...

Post 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 :?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post 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.
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post 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
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post by DSM »

Thanx guys, took enygma's advice, worked like a champ.
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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..
Post Reply