Ashoar Posted April 8, 2009 Share Posted April 8, 2009 I am having a bit of trouble finding out whether a user is logged in or out so that i can display a lit of the registered users online. I have this section of the code: $query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['username'])."';"; mysql_query($query); $query = "INSERT INTO online ( username, time ) VALUES ( '".mysql_real_escape_string($_SESSION['username'])."', '".time()."' );"; mysql_query($query); mysql_close($database_connection); } What kind of Session will i need at the top to find out if they are logged in? I tried using: session_start(); if($_SESSION['s_logged_n'] == 'true'){ But that does not work. I believe i need to verify the users Username and password through the Session function. How would i do this? Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/ Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 Use a timestamp to determine who's online, display those times say within the last 5 mins. Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804575 Share on other sites More sharing options...
Ashoar Posted April 8, 2009 Author Share Posted April 8, 2009 Yes i have a small section that displays in the login that adds to the db at what time they login, and also one for when they log out. But this part here displays on every page to see if the session is still active. So what SESSION function would i use at the top of this script to get the session? Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804578 Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 The time determines if they are still active. Log a timestamp everytime they do something. Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804581 Share on other sites More sharing options...
Ashoar Posted April 8, 2009 Author Share Posted April 8, 2009 So would i use a variable containing the session though? $online = $_SESSION('username') ? And then grab the timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804583 Share on other sites More sharing options...
mrMarcus Posted April 8, 2009 Share Posted April 8, 2009 if($_SESSION['s_logged_n'] == 'true'){ where di you come up with that? there are no predetermined $_SESSION variables in PHP .. that i know of anyways. session vars are what YOU make them. what i would do is for every successful login, create a session like so : //upon successful login .. meaning, have this $_SESSION var declared within your login script. $_SESSION['logged_in'] = true; now, you can use throughout the site as : if ($_SESSION['logged_in']) { //do member stuff; } else { //no access. } Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804594 Share on other sites More sharing options...
Ashoar Posted April 8, 2009 Author Share Posted April 8, 2009 I already have all that. What i was wanting was to constantly update a list of every user that is currently logged in so i could display a list of logged in users. if($_SESSION['s_logged_n'] == 'true'){ That can be used quite well on a page that can only be accessed by users that are logged in. Does the same as what you said: if ($_SESSION['logged_in']) Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804598 Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 Trying to determine their login status by the Session is not a reliable method, which is why I suggested storing a time stamp. Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804623 Share on other sites More sharing options...
Ashoar Posted April 8, 2009 Author Share Posted April 8, 2009 How would i go about doing that with my current script? Quote Link to comment https://forums.phpfreaks.com/topic/153164-logged-in-online/#findComment-804630 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.