PHP 标头重定向失败的 if 语句

发布于 2024-12-01 00:03:54 字数 825 浏览 1 评论 0原文

我有一些代码来记录已经尝试了多少次密码尝试,如下所示:

<?php
/* Access denied */
$_SESSION['error'] = "invalidlogin";

if (isset($_SESSION['tries']))
{
    if ($_SESSION['tries'] == 5)
    {
        //TODO: Redirect somewhere nicer and more descriptive
        header("location:/index.php");
    }
    else
    {
        $_SESSION['tries']++;   
    }
}
else
{
    $_SESSION['tries'] = 0; 
}

header("location:/login.php");
?>

我知道它正在计数到五,然后停止,因为我通过在login.php上回显 $_SESSION['error'] 来测试它

PHP 从不重定向它命中

header("location:/index.php");

但它总是继续前进,然后重定向到

header("location:/login.php");

如何让 PHP 在命中 index.php 重定向后立即重定向?

我是否只在index.php 重定向后设置一个布尔值并在login.php 重定向上检查它?

编辑:为了澄清,我已经定义了一个 session_start();不用担心。

I have some code to log how many password attempts have been tried that looks like this:

<?php
/* Access denied */
$_SESSION['error'] = "invalidlogin";

if (isset($_SESSION['tries']))
{
    if ($_SESSION['tries'] == 5)
    {
        //TODO: Redirect somewhere nicer and more descriptive
        header("location:/index.php");
    }
    else
    {
        $_SESSION['tries']++;   
    }
}
else
{
    $_SESSION['tries'] = 0; 
}

header("location:/login.php");
?>

I know it is counting up to five and then stopping cuz I tested it by echoing $_SESSION['error'] on login.php

PHP never redirects when it hits

header("location:/index.php");

But it always keeps going and then redirects to

header("location:/login.php");

How can I get PHP to redirect as soon as it hits the index.php redirect?

Do I just set a boolean after the index.php redirect and check it on the login.php redirect?

EDIT: To clarify, I have already defined a session_start(); don't worry about that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

﹉夏雨初晴づ 2024-12-08 00:03:54
header("location:/index.php");
exit();
header("location:/index.php");
exit();
够运 2024-12-08 00:03:54

重定向仅在发送到客户端浏览器并且浏览器发起新请求(终止当前请求)后才会生效。简单地调用 header() 不会启动重定向 - 您的脚本将继续运行,直到正常退出、连接关闭并且网络服务器关闭脚本,或者您显式终止执行。

如果您希望重定向标头立即生效,则需要终止脚本,这会导致刷新任何挂起的输出(包括重定向标头):

header("Location: ...");
exit();

A redirect only takes effect AFTER it's been sent to the client's browser and the browser initiates a new request (which terminates the current request). Simply calling header() doesn't initiate the redirect - your script will keep running until it exits normally, the connection is closed and the webserver shuts down the script, or you explicitly terminate execution.

if you want a redirect header to take effect immediately, you need to terminate the script, which causes a flush of any pending output (including the redirect header):

header("Location: ...");
exit();
青衫负雪 2024-12-08 00:03:54

根据 PHP 文档,调用 exit();< 是一个很好的做法/code> 调用 header();

Per the PHP Docs it's good practice to call exit(); after calling header();

披肩女神 2024-12-08 00:03:54

首先,你的整个方法是有缺陷的。您不应在登录失败时进行重定向,这可能会导致安全问题。您应该确保您的脚本在同一请求中包含正确的子页面。

尽管如此,从技术上讲,您的问题的答案是,如上所述,使用 exit()。

First of all, your entire method is flawed. You should not do a redirect upon login failure, that might cause security issues. You should see to it that your script includes the proper sub-page within the same request.

Although, technically the answer to your question is, as stated, to use exit().

本王不退位尔等都是臣 2024-12-08 00:03:54

你忘记开始会话了!!!!
在 header("location:/index.php"); 之后使用 exit 或 die不继续剩下的

u Forgot to start the session !!!!
use exit or die after header("location:/index.php"); to do not continue the rest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文