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
https://forums.phpfreaks.com/topic/213433-manual-php-session-timeout/
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();
}
?>

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.