Jump to content

Sessions - Displaying logged in users.


Beauford

Recommended Posts

I have a login script which uses sessions and all works fine, but I want to be able, as the administrator, to see what users are logged in at a particular time. So if Bob, Sally, and Rubin are logged in, I want to be able to see that. This is just for me, the administrator. I don't want or care if other users can see who is logged in.

 

I have tried searching around for this and all I can find is basic tutorials on sessions, nothing that actually answers my problem.

 

Any help is appreciated. Eventually too I would like to have a timeout to remove inactive users, but one thing at a time.

 

Thanks

 

B

Link to comment
Share on other sites

Well. Do something like this. Have a spot in the database called Logged in. Set it as default no. Then, when they log in, in the 'you are now logged in' part of the login script, put a mysql query that will update the db and change them to yes.

 

What happens then when the user is put into the witness protection program and doesn't log out properly. They would still show as logged in.

 

Is there not just a way for an administrator to query the sessions and parse out the users name and then display them.

 

Thanks

Link to comment
Share on other sites

It shouldnt matter about logging out properly. Like revraz said, everytime a user actions update the time in the database, then query it to whatever time you want, for example users online within the last 5 minutes.

 

Thats how i do things, works perfect.

Link to comment
Share on other sites

It shouldnt matter about logging out properly. Like revraz said, everytime a user actions update the time in the database, then query it to whatever time you want, for example users online within the last 5 minutes.

 

Thats how i do things, works perfect.

 

What Constitutes an action. If I have a user viewing a 12 minute video, then he/she is doing nothing to trigger an action other than the original action to start the video. I need real time accounting. Even if the user does nothing for 15 minutes, I want to know that they are logged in.

 

Again, is there no way to do this with the sessions that are already being used. Seems like an ovesight in PHP if something like this can't be done.

 

Thanks

Link to comment
Share on other sites

So how are they logged out?

 

I think that it is best to write the script using PHP.  Exactly what Xyphon suggested.

 

He or She have to write a script that will log the user out after so many minutes. I actually have

a script that does it, but this isn't a giveaway forum. 

Link to comment
Share on other sites

Unfortunately for us, HTTP is stateless.  This means there's no way for PHP to know if a user is viewing a generated page since the page is given to the user, and then PHP moves on.

 

The only way I've ever seen short of AJAX to see if a user is 'online' or not is simply a time check....

 

In the following script, assume $uid is the user id and that the sql is setup.

 


function UpdateAction($uid) {
    return mysql_query("UPDATE users SET last_act = ".time()." where user_id = '{$uid}'");
}

//then on every page, to mark the user as having done something, you would do:

<?php

include 'SomePageThatHasTheUpdateActionFunction.php';
UpdateAction($uid);
//page content here
?>

 

Then to find the online users, you would do:

 

$min = time() - 600;
$q = msyql_query('SELECT COUNT(uid) FROM users WHERE last_act > {$min}');

 

The only problem with this is, if you have a logout page, obviously you know when someone logged out if they go through that, but this doesn't accomodate for that....  You would most likely want to have a logged_out column, or if you only planned to use the last_act column for users online, you could just set that to 0.

 

 

And yes, I know you want it perfect, but that's impossible, even if you were to communicate directly with Apache somehow.  (Well actually you could see them downloading videos, but if the vid finished downloading before they finished watching, you wouldn't see it.)

Link to comment
Share on other sites

Just a thought, as I'm terrible at javascript, but I remember there's a javascript action that executes code when you close the window, you could attempt to get the javascript to execute some php to make changes to a database. However, that would need ajax as previously mentioned. Simply put, there's no easy way to check what users are currently using sessions as that's a base limitation of a server scripting language like php.

Link to comment
Share on other sites

Actually, you could find session files, deserialize the data and find the username to see who was online, but there's multiple things wrong with that approach:

 

-Depending on where sessions are stored, you may not have access to them.

-It would not scale well, for example if you had 3 thousand files, that would take a while to open, unserialize and read each one

-It could be wrong too!

 

Sessions stay alive for X seconds without being accessed, so technically Bob could go to site a, see one page, close the window and X-1 seconds later, his session would still be open even though he had been gone quite a while.

 

 

Simply put, there's no way with HTTP to know exactly who is on your site at any given moment, but there are ways to know around when they were there.

Link to comment
Share on other sites

i have the following structure on my site.

 

i log every time a user requests a page ,and its stored in page requests.

 

it stores the IP - User_ID, loggin_Id and the page name in the Database

from there i can download and look at the general movements of people acors my site.

 

 

 

what you woudl do is do a query based on the at table ,looking for page requrests int eh last XX minutes - and then sort out all the users etc.

 

** makes your you make it so that your looking for pages that are User_Login only

 

 

gdlk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.