mady Posted June 16, 2009 Share Posted June 16, 2009 This is my code to log out, everything works fine but I want to add auto log out if no activity for lets say 30 mins how can I do that? <?php session_start(); require('dbcon.php'); $usroffline = mysql_query("UPDATE ab_users SET online = '' WHERE username = '$_SESSION[ab_username]'") or die(mysql_error()); unset($_SESSION['ab_logged']); unset($_SESSION['ab_username']); unset($_SESSION['ab_passwrd']); unset($_SESSION['ab_uname']); unset($_SESSION['ab_email']); unset($_SESSION['ab_rank']); unset($_SESSION['ab_eusername']); unset($_SESSION['ab_euname']); unset($_SESSION['ab_epasswrd']); unset($_SESSION['ab_eemail']); unset($_SESSION['ab_erank']); header("Location: index.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/ Share on other sites More sharing options...
mady Posted June 16, 2009 Author Share Posted June 16, 2009 btw inside my index code I use session_start; and session cache expire; <?php session_start(); session_cache_expire(1); if($_SESSION['ab_logged'] == "TRUE"|$_SESSION['ab_username'] != ""|$_SESSION['ab_passwrd'] != ""){ header("Location: home.php"); } ?> ... my login code ?> Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857204 Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 You will need to set a 'last activity' session variable. Update this at the top of each page with the current time. Before the update, do a check to see if 30 minutes has passed since the last time. If it has, then run your logout script. Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857229 Share on other sites More sharing options...
mady Posted June 16, 2009 Author Share Posted June 16, 2009 that's seems to be too difficult; is there way to unset this information after they close the browser window? Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857250 Share on other sites More sharing options...
premiso Posted June 16, 2009 Share Posted June 16, 2009 Unless you have modified something, all session data should be trashed at the close of the browser. It is default in PHP. You can modify this value in the php.ini or ini_set ini_set("session.cookie_lifetime", "1800"); // set cookie to expire in 30 minutes That should timeout your session cookie, thus killing the session in 30 minutes, or 1800 seconds. If the browser is closed, and re-opened your session will still be active unless the browser clears cookies. Set that value to 0 to have it anytime the browser is closed the session gets erased. Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857255 Share on other sites More sharing options...
mady Posted June 16, 2009 Author Share Posted June 16, 2009 Thanks for your reply appreciated. the issue is not their logging off, once they close the browser they are not logged in but the vars i mentioned above in logout.php doesn't get reset. in my php.ini its already this. ; Lifetime in seconds of cookie or, if 0, until browser is restarted. session.cookie_lifetime = 0 Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857274 Share on other sites More sharing options...
premiso Posted June 16, 2009 Share Posted June 16, 2009 the issue is not their logging off, once they close the browser they are not logged in but I want to add auto log out if no activity for lets say 30 mins how can I do that? Ok, so you are just contradicting yourself there. What is the real issue you are having/want solved? You want it, that when they close the browser and come back, if they have not logged out, their session information is still there? But after 30 minutes you want them to be auto logged off? If so, the information I provided to you on the lifetime being set to 1800 would allow for that. The lifetime being set to 0 means, once the browser is closed the session data is killed, removed gone poof! Setting that to 1800 would keep the session data for 30 minutes after no activity, despite if they closed the browser or not. If the 30 minutes is up, then they would have to re-login. Either I am missing what you want, or you do not know what you want...please elaborate on what you want to do exactly. Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857285 Share on other sites More sharing options...
mady Posted June 16, 2009 Author Share Posted June 16, 2009 Thanks for your input, actually you misunderstood it. I'm already using session and it work's perfect whenever they close the window their session expires but I've few var's set for instant this $usroffline = mysql_query("UPDATE ab_users SET online = '' WHERE username = '$_SESSION[ab_username]'") or die(mysql_error()); I use this infos in my scripts like. if($row['online'] == "YES"){ echo 'currently logged'; } so now if they haven't properly logged out this information remains the same and hence wrong information on many places. Thanks you P.S I hope I made it more clear this time Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857302 Share on other sites More sharing options...
premiso Posted June 16, 2009 Share Posted June 16, 2009 So your database is getting out of sync when they have logged out. You will need a cron job and a timestamp in the database that has the row "online" in it, such as "lastactivity" then the cron job would go through that table and if last activity is past x amount of time, change online = "no" for most online scripts it is a 5 minute interval. So if their last activity was 5 minutes, set online to no. You may want it at 30 minute intervals. Look into a CRON Job of Linux or Scheduled Task for windows to accomplish this. Also you will want to look into PHP CLI (command line interface). Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857306 Share on other sites More sharing options...
mady Posted June 16, 2009 Author Share Posted June 16, 2009 I'm familiar with cronjobs but I don't think I can do this task with php, the one you just mentioned is there any other way to auto recall logout.php after lets say 2 hours whenever a user log in and/or any other way to reset these vars. Thanks alot man for your time Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857310 Share on other sites More sharing options...
xcoderx Posted June 16, 2009 Share Posted June 16, 2009 Set time for session isnt that want u want? Yes u can set time for session if no activity fa a mind d session gets destroyd? Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857411 Share on other sites More sharing options...
cs.punk Posted June 16, 2009 Share Posted June 16, 2009 Heres my code I use... Don't know if it will help but anyway <?php include "mysqlcon.php"; $session_id = session_id(); $time = time(); $time_period = 172800; //time in seconds $expire_time = $time + $time_period; $mysqli_con = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname") or die (mysqli_error($mysqli_con)); $sql_d_expired = "DELETE FROM users_online WHERE expire_time <'$time'"; $mq_d_expired = mysqli_query($mysqli_con, $sql_d_expired) or die (mysqli_error($mysqli_con)); $sql_s_sessionid ="SELECT * FROM users_online WHERE session_id='$session_id'"; $mq_s_sessionid = mysqli_query($mysqli_con, $sql_s_sessionid) or die (mysqli_error($mysqli_con)); $row_count = mysqli_num_rows($mq_s_sessionid); if ($row_count == "0") {$sql_i_online = "INSERT INTO users_online (session_id, expire_time) VALUES('$session_id', '$expire_time')"; $mq_i_online = mysqli_query($mysqli_con, $sql_i_online) or die (mysqli_error($mysqli_con)); } else {$sql_u_online = "UPDATE users_online SET expire_time='$expire_time' WHERE session_id = '$session_id'"; $mq_u_online = mysqli_query($mysqli_con, $sql_u_online) or die (mysqli_error($mysqli_con)); } $sql_s_online="SELECT * FROM users_online"; $mq_s_online = mysqli_query($mysqli_con, $sql_s_online) or die (mysqli_error($mysqli_con)); $count_user_online = mysqli_num_rows($mq_s_online); if ($count_user_online == 1) {$users_online = "$count_user_online user online"; } else {$users_online = "$count_user_online users online"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857414 Share on other sites More sharing options...
mady Posted June 16, 2009 Author Share Posted June 16, 2009 Thanks CS punk Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857470 Share on other sites More sharing options...
ict_ashley Posted June 17, 2009 Share Posted June 17, 2009 An even easier way to do this: <script type="text/javascript"> setTimeout("window.location='logout.php'", 100*60*15); </script> Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857677 Share on other sites More sharing options...
cs.punk Posted June 17, 2009 Share Posted June 17, 2009 No problem, just add 'include "users_online.php";' or which ever file you save it as. Also remember to add ' session_start(): at the begining of every page! Quote Link to comment https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857788 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.