Php end session after inactivity

  • Automatically Timeout-Logout Destroy Session inactivity Using PHP
    • db_conn.php
    • emp_login.php
    • emp_welcome.php
    • emp_logout.php
    • Related posts

In this Post We Will Explain About is Automatically Timeout-Logout Destroy Session inactivity Using PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Automatic session timeout-logout using PHP after 3 MinutesExample

In this post we will show you Best way to implement Clear session after 15 minutes of user inactivity using php, hear for User Login Session Timeout Logout in PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

db_conn.php

connect_error);
}
?>

emp_login.php

<h2>User Login</h2>
<form method="post">
<br /><br />
<br /><br />

</form>


emp_welcome.php

<title>Welcome to sample example of Time Session Login</title>


 60) // set here to Time in Seconds
 {
 header("location:emp_logout.php");
 }
 else
 {
 $_SESSION['emp_last_time'] = time();
 echo "<h3 align='right'>Welcome ".$_SESSION["employer"]. "</h3>";
 echo "<h2 align='right'>Automatic PHP to Logout after just 1 minute of session_destroy.</h2>";
 echo "<p align='right'><a href='emp_logout.php'>Logout</a></p>";
 }
}
else
{
 header('Location:emp_login.php');
}
?>


emp_logout.php

Example

I hope you have Got What is Automatic session timeout-logout using PHP after 3 Minutes
And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

PHP Automatic Session Expire after X Minutes of Inactivity/Idle time

 Automatic session timeout/logout using php

Session timeout is a notion and the only way you make you sure that no session ever will survive after X minutes of inactivity. Session timeout or Session expire depends on the server configuration or the relevant directives (session.gc_maxlifetime) in php.ini.

Typically the default is 1440 seconds(24 minutes), but you can alter the default to something else. Below are some Session configurations.

http://php.net/manual/en/session.configuration.php

You can update this easily and without writing custom logic.

If your sessions are implemented with cookies (which they probably are), and if the clients are not malicious, you can set an time limit on the session duration by tweaking certain parameters. If you are using PHP’s default session handling with cookies, setting session.gc_maxlifetime along with session_set_cookie_params should work for you like this:

// server should keep session data for 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client remember their session id for exactly 1 hour
session_set_cookie_params(3600);

session_start(); // ready to go!

You can also put this in .htaccess file with a slight change in syntax.

php_value session.gc_maxlifetime 3600

php_value session.gc_probability 1

php_value session.gc_divisor 1

session.gc_probability, and session.gc_divisor directives: PHP has garbage collection it uses to clean up sessions that have expired, otherwise on a site with a lot of users accessing the site could cause a huge amount of session files to be continually generated. Garbage collection does not happen automatically and needs to be incorporated into your system maintenance routines.

You can also make a custom script that automatically logout a user if user is inactive (not performed any action or idle) for X minutes.

1) When user logged in, start session, start session expiry time, like this:

 $_SESSION['expire'] = time() + X*60; 

We took current time, added X minutes in it and stored this in session.

2) At every page check that if X minutes (for above script) have been passed or not make a file as include that in that page, like this:

If yes, clear session and logout, like this:

if(time() > $_SESSION['expire']){
    session_destroy();
    session_write_close();
    session_unset();
    $_SESSION = array();
}

And then redirect to login page.

3) In else statement (if X minutes have not passed), reset time (takes current time and add X minutes in it and restore in session named ‘expire’) stored in session, like this:

else { $_SESSION['expire'] = time()+X*60; }

and do nothing – don’t clear sessions, don’t redirect to login page, so that user may stay at website, as much time as he is active.

You can also do this purely using JavaScript. Start an countdown timer. Then wait for activity and reset this timer. If there is no activity and timer goes off, you can call your logoff sequence.

/* Resets the timer. The timer is reset on events
   (mouse-move,mouse-click,key press,scrolling),
   these events occurs indicates that user is active on the application:
*/

<body onmousemove="reset_interval()" onclick="reset_interval()" onkeypress="reset_interval()" onscroll="reset_interval()">

<script type="text/javascript">
 
//the interval 'timer' is set as soon as the page loads

var timer = setInterval(function(){ auto_logout() }, 20000);

// the figure '20000' (20 seconds) indicates how many milliseconds the timer be set to.

//e.g. if you want it to set 5 mins, calculate 5min= 5x60=300 sec => 300,000 milliseconds.
 
function reset_interval(){

    //first step: clear the existing timer
    clearInterval(timer);
   
    //second step: implement the timer again
    timer = setInterval(function(){ auto_logout() }, 20000);
    //..completed the reset of the timer

}

function auto_logout(){

    //this function will redirect the user to the logout script
   
    if(confirm("You have been logged out from the application, Press OK to login again!")){
        window.location="your_logout_script.php";
    }

}

</script>

Hope it will works, let me know by your valuable comments if you need any more assistance.

How do I expire a PHP session after 30 minutes?

if you want to expire the session after 30 minutes of activity instead of after 30 minutes since start, you'll also need to use setcookie with an expire of time()+60*30 to keep the session cookie active.

Is it possible to set a time expire page in PHP?

By default, a session in PHP gets destroyed when the browser is closed. Session timeout can be customized, to make the user's page inactive after a fixed time. Starting session: The PHP, session_start() function is used to start a session in the web page.

How long do PHP session variables last?

By default, session variables last until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application. Tip: If you need a permanent storage, you may want to store the data in a database.

How can destroy session after some time in PHP?

Destroying a PHP Session A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.