PDA

View Full Version : Show users logged into MySQL database



Xtreamerthaneve
19-10-2008, 06:27 PM
I have a user system that I made, and I was wondering if anyone knew a PHP code that I could use to display all of the people logged into the MySQL database.

Trigs
20-10-2008, 01:12 AM
Username hook?

HabbDance
20-10-2008, 01:18 AM
you mean like a 'Who's Online' type thing?

D-Man22
20-10-2008, 04:32 PM
On every login, use the
<? if ($date_logged_online==('(date(D:M:Y - H:M-15))')-->%){

$database --> $array ==>

$rowselect['user_name_varible'];

$online_usr = $rowselect['user_nmae_varible'];

echo "'.$online_usr.' is ONLINE! <br>";

} else { echo "Your the only one online!"; ?> typa thing? I' poor at PHP, but it works on myne (yes I'm making a usersystem...) I'm assuming your connected to a table and a database... Right?
It should work, Happy PHP! change "$rowselect" to the variable your using to get info from the database table... :D. It should show logged in people from the last 15 minutes, enjoy!


EDIT: $array is the editible variable to the variable ur connecting to table!

Dentafrice
22-10-2008, 12:18 AM
In your users table, have a lastUpdated field.. on each page in the config.php (which should be called on most pages..) have a checkLogin() function which returns true/false.



if(checkLogin() == true) {
updateUser( $username );
}


updateUser can just update that field with time(), then just calculate everyone with a lastUpdated field in a period of say.. 15 minutes.

That should calculate your "users online"

Suspective
22-10-2008, 06:24 AM
Sorry Don't Know about MySQL! :P

Hypertext
22-10-2008, 12:43 PM
In your users table, have a lastUpdated field.. on each page in the config.php (which should be called on most pages..) have a checkLogin() function which returns true/false.



if(checkLogin() == true) {
updateUser( $username );
}


updateUser can just update that field with time(), then just calculate everyone with a lastUpdated field in a period of say.. 15 minutes.

That should calculate your "users online"

Exactly how I do it, thanks to you! I remember back in the day, when I used a boolean to represent on and off. Horrifically, illogical.

@OP: If you mean actual MySQL, as Fazon said use a user login/logout hook.

Btw, a hook is a script that is run on a specific event.

Use it to update a text file, as I'm guessing that would be the best way to retrieve it.

Then your whos online? code should just read off of that text file.

Calon
22-10-2008, 12:45 PM
Sorry Don't Know about MySQL! :P
Now you're just spamming, don't post if you can't contribute.

Just use a session, and on every page it updates the user status with something like Dentafrice said.

Dentafrice
22-10-2008, 12:57 PM
Exactly how I do it, thanks to you! I remember back in the day, when I used a boolean to represent on and off. Horrifically, illogical.

@OP: If you mean actual MySQL, as Fazon said use a user login/logout hook.

Btw, a hook is a script that is run on a specific event.

Use it to update a text file, as I'm guessing that would be the best way to retrieve it.

Then your whos online? code should just read off of that text file.
Why the hell write it to a text file? That's pretty illogical, and hooking it on the login/logout.. that's not going to make an accurate "who's online" if you don't update the last activity they did.. that's just going to make it on the login timestamp.

Sounds pretty "illogical" to me.

Source
22-10-2008, 05:23 PM
The only "accurate" way todo it would be with the timestamp within a database, or make the sessions save to a directory (which can be set in php) then use some PHP code to count all sessions which have been active within a certain time, still uses timestamp but it grabs it from the php session files directly.

I have always done it with a timestamp in a user database, so my second thought could be completly useless.

Protege
22-10-2008, 05:49 PM
Add another column to the Users tablE, call it "LastActivity" - Source talked about this.

Everytime you check if the user is logged in, update that column with the "time()" function

eg:



mysql_query( 'UPDATE `Users` SET `LastActivity` = "' . time() . '" WHERE `id` = "' . $userID . '" LIMIT 1;' );
I do this when I check if the users logged in (exactly the same function name as Caleb uses ;o "checkLogin()")

Now to get all the users who are logged in:


<?php
$mysqlUsers = mysql_query( 'SELECT * FROM `Users`' ) or die( 'Die' );

if( mysql_num_rows( $mysqlUsers ) >= 1 )
{
$loggedIn;

while( $r = mysql_fetch_array( $mysqlUsers ) )
{
$timeOut = 60 * 30; // 30 Minutes (Timeout)

// Below is checking the time difference between their last inactivity and now, then checking it against 30mins ($timeOut )

if( ( $r[ 'LastActivity' ] - time() ) <= $timeOut )
{
$loggedIn .= $r[ 'UserName' ] . ' ';
// ^^ Add to the list of loggedIn users.
}

}

echo $loggedIn;
}

?>Thats a basic timeout for you. I use that to log the user out if they been inactive too long too.

Xtreamerthaneve
23-10-2008, 09:25 PM
Thank you everyone for your help. I do understand what you're all saying, and I have created the table w/ the timestamp in my database. I added those codes to my config page, but for some reason it's not updating the database at all. I think if it was updating it, the other PHP code that Protege gave me would work. Could anyone maybe help me with the first mysql entry to have it actually send the last activity to the db?

Want to hide these adverts? Register an account for free!