Beauford Posted December 15, 2007 Share Posted December 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
Xyphon Posted December 15, 2007 Share Posted December 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 15, 2007 Share Posted December 15, 2007 A good way to do this is log the Login time. Then query say the last 5 mins of the last update. Everytime the user does an action, update this time. Quote Link to comment Share on other sites More sharing options...
Beauford Posted December 15, 2007 Author Share Posted December 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
Dane Posted December 15, 2007 Share Posted December 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
Beauford Posted December 15, 2007 Author Share Posted December 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
papaface Posted December 15, 2007 Share Posted December 15, 2007 Its not an over-sight in PHP. PHP is for server side things. If you want it to update while they're on the page I presume you'd have to use something like AJAX. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 16, 2007 Share Posted December 16, 2007 Use a longer session timeout in the PHP.INI, but then you have a bigger problem since that is a higher security risk. Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 16, 2007 Share Posted December 16, 2007 I'd say AJAX is the way to go here. Here's a good tutorial to get you started: http://marc.info/?l=php-general&m=112198633625636&w=2 Quote Link to comment Share on other sites More sharing options...
Trium918 Posted December 16, 2007 Share Posted December 16, 2007 I think that it is best to write the script using PHP. Exactly what Xyphon suggested. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 16, 2007 Share Posted December 16, 2007 So how are they logged out? I think that it is best to write the script using PHP. Exactly what Xyphon suggested. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted December 16, 2007 Share Posted December 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 16, 2007 Share Posted December 16, 2007 Sounds like more work than just checking their activity time, because you are basically checking time anyways. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 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.) Quote Link to comment Share on other sites More sharing options...
simonj2 Posted December 16, 2007 Share Posted December 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted December 16, 2007 Share Posted December 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.