Redirecting after session timeout.

Started on the old forum in November 2011 · view the original in the archive · old links redirect here

4 replies · 1,005 views · started 15 years ago · 1 min read

4 participants
Showa Newbie · 4 posts #1

I'm trying to redirect to the logout page after a certain elapsed time, momentary set to one minute for testing purposes.

This is my session script:

<?php
session_start();
//session_register("session");
$timeout = 1; // Set timeout minutes
$logout_redirect_url = "logout.php"; // Set logout URL

$timeout = $timeout * 60; // Converts minutes to seconds
if (isset($_SESSION['start_time'])) {
    $elapsed_time = time() - $_SESSION['start_time'];
    if ($elapsed_time >= $timeout) {
        session_destroy();
        header("Location: $logout.php");
    }
}
$_SESSION['start_time'] = time();
?>

The script is in an include file.

The original script only had lines 2 and 3, I added the rest. Sadly it want redirect. Can anybody detect a mistake?

With kind regards,

Ad.

Showa Newbie · 4 posts #3

Thank you Khan,

Refreshing the page where I'm logged in to, is showing now the log out page.

I would like the log out page (or another page) to show automatically after timeout. What do I have to add or change? I thougt the logout_direct_url did this.

arnage Lead · 811 posts #4

Try this and session_destroy(); add in logout.php.

<?php 
session_start();
$logout_redirect_url = "logout.php";
$timeout = 60;
if (isset($_SESSION['start_time'])) {
$elapsed_time = time() - $_SESSION['start_time'];
$the_time = abs(intval($elapsed_time));
if ($the_time < $timeout) {
header('Refresh: '.$the_time.'; URL=./'.$logout_redirect_url);
} else {
header('Location: ./'.$logout_redirect_url);
exit;
}
} else {
$_SESSION['start_time'] = time();
}
?>
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
GumSlone Administrator Principal · 1,690 posts #5

the mistake was in

header("Location: $logout.php");

ive changed it to

header("Location: ".$logout_redirect_url);

now you can define the logout redirect url the way you want to.

<?php
session_start();
//session_register("session");
$timeout = 1; // Set timeout minutes
 $logout_redirect_url = "logout.php"; /* define your logout file ie. logout.php or logout url ie. http://yoursite.com/logout_file.php */
$timeout = $timeout * 60; // Converts minutes to seconds
if (isset($_SESSION['start_time'])) {
    $elapsed_time = time() - $_SESSION['start_time'];
    if ($elapsed_time >= $timeout) {
        session_destroy();
         header("Location: ".$logout_redirect_url);
    }
}
$_SESSION['start_time'] = time();
?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close