Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2008, 11:42 PM   PM User | #1
333qaz333
New Coder

 
Join Date: Dec 2007
Location: NY, USA
Posts: 27
Thanks: 7
Thanked 2 Times in 2 Posts
333qaz333 is an unknown quantity at this point
How to tell if a user is online

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.)
333qaz333 is offline   Reply With Quote
Old 01-22-2008, 11:53 PM   PM User | #2
o0O0o.o0O0o
Senior Coder

 
o0O0o.o0O0o's Avatar
 
Join Date: Jan 2008
Location: C:\Windows\System32
Posts: 1,018
Thanks: 19
Thanked 9 Times in 9 Posts
o0O0o.o0O0o is infamous around these parts
just check whether they have logged in or not

make the flag field in user table which tells they are logged in or not
o0O0o.o0O0o is offline   Reply With Quote
Old 01-22-2008, 11:58 PM   PM User | #3
333qaz333
New Coder

 
Join Date: Dec 2007
Location: NY, USA
Posts: 27
Thanks: 7
Thanked 2 Times in 2 Posts
333qaz333 is an unknown quantity at this point
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.
333qaz333 is offline   Reply With Quote
Old 01-23-2008, 12:35 AM   PM User | #4
o0O0o.o0O0o
Senior Coder

 
o0O0o.o0O0o's Avatar
 
Join Date: Jan 2008
Location: C:\Windows\System32
Posts: 1,018
Thanks: 19
Thanked 9 Times in 9 Posts
o0O0o.o0O0o is infamous around these parts
When they log in set the session variable

Code:
$_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
o0O0o.o0O0o is offline   Reply With Quote
Old 01-23-2008, 12:38 AM   PM User | #5
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
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.
bcarl314 is offline   Reply With Quote
Users who have thanked bcarl314 for this post:
333qaz333 (01-23-2008)
Old 01-23-2008, 01:06 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by o0O0o.o0O0o View Post
When they log in set the session variable

Code:
$_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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
333qaz333 (01-23-2008)
Old 01-23-2008, 01:45 AM   PM User | #7
333qaz333
New Coder

 
Join Date: Dec 2007
Location: NY, USA
Posts: 27
Thanks: 7
Thanked 2 Times in 2 Posts
333qaz333 is an unknown quantity at this point
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?
333qaz333 is offline   Reply With Quote
Old 01-23-2008, 02:02 AM   PM User | #8
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
http://www.php.net/manual/en/ref.session.php
__________________
John
PappaJohn is offline   Reply With Quote
Old 01-23-2008, 02:16 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by 333qaz333 View Post
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
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-23-2008, 02:22 AM   PM User | #10
333qaz333
New Coder

 
Join Date: Dec 2007
Location: NY, USA
Posts: 27
Thanks: 7
Thanked 2 Times in 2 Posts
333qaz333 is an unknown quantity at this point
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.

Last edited by 333qaz333; 01-23-2008 at 02:33 AM..
333qaz333 is offline   Reply With Quote
Old 01-23-2008, 03:58 AM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Mmkay.
You would run against your standard sessions that you would use on a page, for example:
PHP Code:
<?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 Code:
<?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 Code:
<?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:
PHP Code:
$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:
PHP Code:
$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
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 01-23-2008 at 04:02 AM.. Reason: Maybe I should show how to add it with the time on the files...
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
333qaz333 (01-23-2008)
Old 01-23-2008, 11:20 AM   PM User | #12
333qaz333
New Coder

 
Join Date: Dec 2007
Location: NY, USA
Posts: 27
Thanks: 7
Thanked 2 Times in 2 Posts
333qaz333 is an unknown quantity at this point
Thank you very much.

I guess I will stick with the DB method... I don't think I will mess with the session way.
333qaz333 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:45 PM.


Advertisement
Log in to turn off these ads.