Jump to content

how to destroy session automatically after defined time?


rahulvicky00

Recommended Posts

As my title said the question, i want to destroy my session after a particular time if user is being ideal..

currently i am using this code to define my session:

 


session_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['myusername'])) {
header('Location: admin.php');
}

 

Link to comment
Share on other sites

<?php
    session_start();
     
    // timeout after 10 minutes
    $inactive = 600;
     
    $session_life = time() - $_session['timeout'];
     
    if($session_life > $inactive)
    { session_destroy(); header("Location: logoutpage.php"); }
    S_session['timeout']=time();
?>

Link to comment
Share on other sites

You need to be more specific, do you want:

1)  The session to expire after a certain amount of inactivity?  For instance, if they get up and go to lunch, when they come back they'll be logged out?

2)  The session to expire at a specific time after login regardless of their activity.  For instance, they can only browse your site for 4 hours before they're automatically kicked out?

Link to comment
Share on other sites

You need to be more specific, do you want:

1)  The session to expire after a certain amount of inactivity?  For instance, if they get up and go to lunch, when they come back they'll be logged out?

2)  The session to expire at a specific time after login regardless of their activity.  For instance, they can only browse your site for 4 hours before they're automatically kicked out?

 

I need something like "The session to expire after a certain amount of inactivity?  For instance, if they get up and go to lunch, when they come back they'll be logged out?"...

 

Link to comment
Share on other sites

session_set_cookie_params allows you to set the timeout for the user's session cookie.

 

kney's solution accomplishes roughly the same thing at the server level, but note that you need to die() after a header() call.

 

Thanks for a very quick reply but can you give me some coding, actually i am very new in PHP... A example code would be a grate help..

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.