How do I alert admin users when their account has timed out

◇◆丶佛笑我妖孽 提交于 2019-12-12 00:45:04

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/20104231/how-do-i-alert-admin-users-when-their-account-has-timed-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!