...

View Full Version : How to tell if a user is online



333qaz333
01-22-2008, 11:42 PM
I have a webpage where users login. What I am trying to do is create some kind of code so that I know when they are online and when they are offline.

I am thinking about using a SQL db to store it, but I don't know the best way to get around doing this.

Also, I want to stay away from onload and onunloads if possible, as this will cause slow page loading times every time the user goes to a different subpage. (but at this point I will take anything.)

o0O0o.o0O0o
01-22-2008, 11:53 PM
just check whether they have logged in or not

make the flag field in user table which tells they are logged in or not

333qaz333
01-22-2008, 11:58 PM
yes, the only problem is is that if they just click 'X' instead of logout, the db will still say that they are logged in.

o0O0o.o0O0o
01-23-2008, 12:35 AM
When they log in set the session variable


$_SESSION["logged"] = true

or if you want more detail set this variable to a value which uniqly identifies the user e,g
userid



If this variable has that value it means the person is online

If user closes the browser then this variable will have no value

bcarl314
01-23-2008, 12:38 AM
There's really no way to do this in "real time" but what you can do is, if the user is logged in, everytime they load a page, update a "last_action" field in the database. Then, you program your site to query the database for everyone with a last action within 5 minutes (or some other interval). This would be a good "rough" indicator of who's online.

Fou-Lu
01-23-2008, 01:06 AM
When they log in set the session variable


$_SESSION["logged"] = true

or if you want more detail set this variable to a value which uniqly identifies the user e,g
userid



If this variable has that value it means the person is online

If user closes the browser then this variable will have no value
That is not accurate. When the user closes the browser the object in use (session) loses its 'pointer' to say, and the session is no longer retrievable. The session data itself remains on the server until the next garbage collection run by PHP.
DB Driven sessions.
Only way I can think of, similar to what BCarl was getting at. If you clean up your dead sessions after a given period of time, the count is easy.
Alternative method:
Iterate through every session file available (must be stored in a local directory), hope the garbage collection has not gotten to it, and check the last_activity (or something of the sorts) to determine if its still 'current'. Not fun. Trust me.

Go with the DB Sessions, you can probably find a thousand tutorials through google relating to them.

333qaz333
01-23-2008, 01:45 AM
thank you two for your help. I am very greatful.

Honestly, I don't know how to use session variable at all (I think they are like cookies?), so I think I will try the sql table method.

Where can I learn about sessions?

PappaJohn
01-23-2008, 02:02 AM
http://www.php.net/manual/en/ref.session.php

Fou-Lu
01-23-2008, 02:16 AM
thank you two for your help. I am very greatful.

Honestly, I don't know how to use session variable at all (I think they are like cookies?), so I think I will try the sql table method.

Where can I learn about sessions?

You are right, they are like cookies, sort of. Except they are stored on the server filesystem instead of the client system - this makes them more secure than cookies alone.
Generally the two are mixed, a cookie is (by default) to be set on the client machine if it can, that holds a reference to the session on the server's filesystem

333qaz333
01-23-2008, 02:22 AM
Could you give me an example of using sessions in relevance to what I am trying to do?

I am reading tutorials, but i am just not getting sessions yet...


I get how to store the sessions on an individual browser, but how could i get it on the server, so I can tell who is online or not.

I would like to do something similar to this site, where all users can tell who is online now.

Fou-Lu
01-23-2008, 03:58 AM
Mmkay.
You would run against your standard sessions that you would use on a page, for example:


<?php
session_start();
$_SESSION['lastActivity'] = time();

...
?>

Now, add to it a location (or do it via .ini directly or using an .htaccess file if you have it available), relative to the calling script:


<?php

ini_set('session.save_path', '/my/path/to/session/storage');
session_start();
...


Now for the fun part. You got to count those sessions:


<?php

$i = 0;
foreach (glob('/path/to/session/storage/*') AS $sessions)
{
$i++;
}

Voila, simulated counter. This doesn't even keep track of the record time, for that you will need to unserialize the packed data in the session, read the 'lastActivity' hash value, determine if its still online or not, and increment the counter.
Decided to edit this part in to give you an example for how to check to see if its a current session. Not sure if this will actually work how I expect it to, but will give it a shot:


$i = 0;
foreach (glob('/path/to/session/storage/*') AS $sessions)
{
$sess = unserialize($sessions);
if (is_array($sess) && ($time = array_key_exists('lastActivity', $sess)))
{
if ($time > time() - TIMEOUTCONST)
{
// Still considered online
$i++;
}
}
}


As you can see, this isn't overly pleasant, and I don't want to throw a script together for this purpose >.<.
DB's are a lot easier to deal with (but harder to put together until you get the hang of it). In case you are wondering, I use database sessions exclusively now, haven't used the predefined sessions in years (except to test on them and keep as 'up to date' as I can with them). To count off a DB driven session:


$qryStr = "SELECT count(*) FROM {SESSIONTABLE} WHERE `lastActivity` > time() - 3600";
$numOnline = $sqlObj->query($qryStr);

All done on that - obviously this is using a custom sql object and query itself won't do, but a simple fetch is all that is required on top of it. The 3600 is mearly a constant number to subtract, think of it as a 'timeout', anyone with no activity after 3600 seconds is considered to be disconnected and not included in the help.
Forum software (like this site) use Database driven session management, thats why the counts are so easy to preform.
Hope this helps you out, and I'm pretty sure I mentioned it already - just search google (or maybe even here not sure if someone has posted one) for a 'database session management in PHP' script or tutorial. There are a lot out there.
Good luck :thumbsup:

333qaz333
01-23-2008, 11:20 AM
Thank you very much.

I guess I will stick with the DB method... I don't think I will mess with the session way.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum