Jump to content

Session Destroy Timer


Tazerenix

Recommended Posts

I'm working on my own file hosting system right now with the help of my friend. I've coded in a user registration/login function already and im using session_start() and $_SESSION variables.

I've looked up several ways of automatically deleting the session after a certain time but i don't really understand them to well.

I've also tried putting a code at the top of each page like:

<?php
$_SESSION['last_active'] = time();
$sessiontime = 900;
if ($_SESSION['last_active'] >= $sessiontime)
{
session_destroy();
header('Location: login.php?error=1');
}
?>

But with a few more lines and other stuff.

I've seen people suggest using code like this for auto logout but the problem with this is that it won't continuously check this all the time.

I'd like to know if someone could give some detailed example code of how to destroy the session cookie and redirect to the login page again after a certain amount of time. But it also has to have the time until deletion reset on every page load. I'm really not too sure how to do this so as much explanation and help as possible would be greatly appreciated.

And will this also work if the user still closes the window? So if the user starts a Logged in Session then closes the window and comes back 930 seconds later instead of 900 will the cookie still have been deleted?

 

Thanks for your answers if you give any  :D

 

ps: Yay, first post, just joined here :D

Link to comment
Share on other sites

That code doesn't make much sense, you need to set the session after checking it, not before or it will never expire

 

try

<?php
$expiretime = 900;
if ($_SESSION['last_active']+$expiretime >=  time()){
session_start();
$_SESSION = array();
if (ini_get("session.use_cookies")) {
	$params = session_get_cookie_params();
	setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
}
header('Location: login.php?error=1');
}else{
$_SESSION['last_active'] = time();
}
?>

Link to comment
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.