duanbu1998 2013-10-21 08:21 采纳率: 0%
浏览 79
已采纳

PHP会话到期但页面刷新后我没有被重定向

Problem is, when my session expires I have to reload page twice to get back to login page. here is my .htaccess file

php_flag display_errors on
php_value error_reporting 9999

RewriteEngine On
Options -MultiViews
AddDefaultCharset UTF-8
php_value session.gc_maxlifetime 5
php_value session.gc_divisor 1
php_value session.gc_probability 1
php_value session.cookie_lifetime 5
php_value session.save_path /path/to/session/folder

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

I'm using some custom MVC framework and I'm calling handleLogin() function in controllers __construct function

function __construct() {
    parent::__construct();
    Auth::handleLogin();
}

And finally here is handleLogin() function

public static function handleLogin() {
    @session_start();
    if (isset($_SESSION['loggedIn'])) {
        $logged = $_SESSION['loggedIn'];
    } else {
        session_destroy();
        header('location: ' . URL . 'login');
        exit;
    }
}

What do I need to change to redirect user to Login page after FIRST refresh when session expires?

Thanks in advance!

P.S. I'm running it on Debian

UPDATE: here is logout function and it works fine, when user clicks it he is immidiately redirected to login page

function logout()
{
    session_destroy;
    header('location: ' . URL .  'login');
    exit;
} 

UPDATE #2 I got some screenshots for better problem description

1) When session is active and everything works fine enter image description here

2) session expired - user clicks link or refresh page (after first refresh) as you can notice in firebug login is loaded but it's not shown enter image description here

3) after 2nd refresh enter image description here

One more thing is.. when using default php.ini settings (session lifetime: 1440 sec) everthing works fine with no problem in redirecting

  • 写回答

2条回答 默认 最新

  • dongquxiao8545 2013-10-22 06:54
    关注

    I found what caused error, but I'm not exactly sure why was it causing such a stupid bug. I had session_set_cookie_params(0); earlier in my code, and while my browser was opened all the time i changed my code, removed session_set_cookie_params(0); and added php_value session.gc_maxlifetime 5. That's what caused conflict. One side expected session to last until browser closes and other side terminated session after 5 sec. I cleared my cache 100 times and nothing happened until i closed my browser and reopened project again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴