Jump to content

Recommended Posts

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");
?>

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857255
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857274
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857285
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857302
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857306
Share on other sites

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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/162395-auto-log-out/#findComment-857414
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.