需要刷新页面才能删除会话

时间:2014-01-28 17:33:51

标签: php

在这段代码中,我试图禁止客户端,如果他/她/它做了很多(10)登录请求3分钟。问题是3分钟后用户必须刷新页面2次。我可以看到它进入if语句但我无法找到解决方案的原因。我觉得我已经过度编码了。

if($this->sessions->get_data("wrong_login")>10){
            if(!isset($_SESSION["ban_time"])){
                $this->sessions->set_data("ban_time", time());
            }else
            {
                if(time() - $this->sessions->get_data("ban_time") > 180){ // 180 seconds
                    $this->sessions->remove("ban_time");
                    $this->sessions->remove("wrong_login");
                }
            }

            // The message if user still banned
            die("Banned for 3 minutes!");
        }

我希望我能说出问题..

编辑:此代码是寄存器控制器构造的内部。

2 个答案:

答案 0 :(得分:1)

在你的IF语句之前,如果时间到了,则添加另一个检查ban_time会话的if语句,如果是,则将wrong_login会话设置为0.

if($this->sessions->get_data("ban_time") < time())
{
    $this->sessions->remove("ban_time");
    $this->sessions->set_data("wrong_login", 0);
}

删除你在那里的其他声明。

也忘了提!当你设置禁令时间时,它应该是时间()+ 180。

if(!isset($_SESSION["ban_time"])){
    $this->sessions->set_data("ban_time", time()+180);
}

答案 1 :(得分:0)

使用header功能。

e.g。

header("Location: /path/to/some/file.php?error=Banned for 3 minutes.");

然后在file.php上你可以这样做:

<?php

// Parse error
$error = isset($_GET['error']) ? $_GET['error'] : '';

// Display error (if any) and stop executing the rest of the code.
if (!empty($error)) {
    exit($error);
}

?>

如果你已经开始输出......这将无效。

相关问题