Jump to content

Manual PHP Session Timeout?


Far Cry

Recommended Posts

Hi there, I am wondering if you guys can help me make it so my session times out after a previously set time. I have researched this and found no luck. Here is my code....

 

<?php
session_start();

$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
?>

 

 

Thanks in advance!

Link to comment
Share on other sites

I think what you want is to just set the cookie experation time:

setcookie(CookieName, $data, time()+1800);

or do a timer script like this

Timer.php

<?php
session_start();
if($_SESSION['session_count'] == 0) { 
$_SESSION['session_count'] = 1;
$_SESSION['session_start_time']=time();
} else {
$_SESSION['session_count'] = $_SESSION['session_count'] + 1;
}

$session_timeout = 1800;

$session_duration = time() - $_SESSION['session_start_time'];
if ($session_duration > $session_timeout) { 
session_unset();
session_destroy();
session_start();
session_regenerate_id(true);
setcookie("CookieName", 0, time()-3600);
setcookie("Cookie2Name", 0, time()-3600);
$_SESSION["expired"] = "yes";
header("Location: ./"); // Redirect to Login Page
} else {
$_SESSION['session_start_time']=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.