dqqpf32897 2014-08-05 17:23
浏览 53
已采纳

在一段时间不活动后将用户注销

I want user to be logged out after some time inactivity. I want this php code to run automatically after some time of user inactivity. It must happen without refreshing the page.

<?php
    if (isset($_SESSION['user_login_status'])) {
        $max_time = 5; // Maximun inactive time(this time is set in seconds )
        $current = time(); // Current time on server

        if (!isset($_SESSION['Inactive']))
        { // Create session inactive;
          Session::set('Inactive', time()); // Create session inactive;
        } else {
            $session_life = $current - $_SESSION['Inactive'] ;

           if ($session_life > $max_time )
            {

             Session::destroy(); // This is a function that destroys all sessions and logging out the user
             header('location: index.php'); // Redirects to some kinda page 
            } else {
                $_SESSION['Inactive'] = time();
            }

        }
    }
?>

This php code is working and user is logged out after 5 seconds when I refresh the page. But I need this code to be runned after those 5 seconds of inactivity and it should redirect to another page. I have tried some ajax code but it didn't worked.

Any suggestions how can I Run that php code after some time?

A lot of misspelled words. Sorry for that.

  • 写回答

1条回答 默认 最新

  • dougu2036 2014-08-05 17:42
    关注

    Modify the code according to your needs. What this code would do is that if the user refreshes the page within 5 second, the timer will reset and start the count again. If user does not refresh/reload the page within 5 seconds, ajax call will be made to your controller action to log the user off. Return a new url to the ajax call to automatically redirect user to a new page. [FYI, I do not like automatic logoffs, specially such short ones. Of course, most Web servers have session timeouts. I would rather go with those timeouts.]

    // add these functions at the bottom of the output html page within <script> tags
    // YOU SHOULD CALL setLogoutTimer FUNCTION ON MOUSEMOVE OR SOME USER ACTIVITY EVENT.
    // otherwise user will be logged out even when the user is doing something on the page
    
    
    
            setLogoutTimer();
    
    
        function setLogoutTimer() {
            var myTimeout;
            if (window.sessionStorage) {
    
                myTimeout = sessionStorage.timeoutVar;
                if (myTimeout) {
                    clearTimeout(myTimeout);
                }
    
            }
    
            myTimeout = setTimeout(function () { logoutNow(); }, 5000);  //adjust the time. 
            if (window.sessionStorage) {
                sessionStorage.timeoutVar = myTimeout;
            }
        }
    
        function logoutNow() {
            if (window.sessionStorage) {
                sessionStorage.timeoutVar = null;
            }
            //MAKE AN AJAX CALL HERE THAT WILL CALL YOUR FUNCTION IN 
        // CONTROLLER AND RETURN A URL TO ANOTHER PAGE
    
        $.ajax({
                url: 'YOUR CONTROLLER ACTION URL',
                cache: false,
                 async:false,
                type: 'POST',
                success: function (msg) {
                    window.location.href=msg;  //msg is the url of another page returned by the controller
    
    
                }
            });
    
    
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程