Question

The website I program for is a high school newspaper. Lately, the admin page has been having a trending issue. When by the time users have created an article, they have been logged out due to a set time limit. I want to program in an alert to tell users they have been logged out so that they can copy their article and paste it back in place. This is our timeout code:

    // Kill Session if session end exists.
    if ($_GET['endsession']) {
        unset($_SESSION['name']);
        unset($_SESSION['access']);
        unset($_COOKIE['username']);
        setcookie("username","",time() - 3600);
        session_destroy();
        header("Location: http://" . $_SERVER['HTTP_HOST'] . "/admin.php");
    }

I have tried placing a javascript alert under the [ session_destroy(); ] and above the [ setcookie("username","",time() - 3600); ], but that resulted in nothing happening. I also tried placing it above the [ session_destroy(); ], but that made the page not even time anyone out.

How do I get a simple Javascript alert to run when the function [ session_destroy(); ] runs?

Was it helpful?

Solution

How about you check - before unsetting the session - if there are, let's say, five minutes left?

if($_GET['endsession'] < time() + 5*60) {
    //alert the user
}

if($_GET['endsession']) {
    //your code to unset the session
}

Assuming $_GET['endsession'] contains a timestamp. If this is not the case you may as well transform it or whatsoever, but I think the general idea is clear.

OTHER TIPS

Maybe in your body tag:

<body onload="window.setTimeout(function() {window.alert('You have been logged out');},600000);">

This will put an alert in the browser at 10 minutes (600 seconds / 600,000ms) saying "You have been logged out".

You can also do it in the body for the same effect:

<script>window.setTimeout(function() {window.alert('You have been logged out');},600000);</script>

http://www.w3schools.com/jsref/met_win_settimeout.asp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top