简单的条件 - 未按预期工作 - 最后与 die() 一起使用吗?

发布于 2024-10-31 06:25:11 字数 1407 浏览 0 评论 0原文

我不敢相信我竟然一直盯着下面的代码:

public function send($boxMemUsed, $boxMemAllocate) {
    $this->timeSent = gmmktime();
    if ($boxMemUsed >= $boxMemAllocate)
        header("Location: ".HOME_PAGE.PM_PAGE."?view=inbox&status=Memory full - please delete old messages");
    $validation = $this->_validateMessage();
    if ($validation == 'Invalid User')
        header("Location: ".HOME_PAGE.PM_PAGE."?view=compose&status=Errors&error=".$validation);

---->>>如果我在这里添加 die() ,它会重定向到上面的标题,就好像 if 语句正常工作一样。

    else
        $sender = $this->_database->sendPMessage(get_object_vars($this));
    if ($sender)
        header("Location: ".HOME_PAGE.PM_PAGE."?view=sentbox&status=Message Sent!");
    else
        header("Location: ".HOME_PAGE.PM_PAGE."?view=compose&status=Errors&error=Send Error");
    }

如果我在这里添加 die,在结束函数 curly 之前,它会捕获 if($sender) 条件。

我正在编写的私人消息程序中的简单功能。好的,所以,我进入实时页面,没有错误,我输入了错误的用户名,它应该被 $this->_validateMessage() 调用捕获 - 是的,我直接将结果 var_dump'd 和死()了。然后我检查了 if 条件是否正确地捕获了它,它是 - 并且它重定向了我......?但随后我删除了测试 var_dumps 和 if 末尾的骰子,它不会被相同的 if 条件捕获(我们正在讨论的 if 是 if ($validation == 'Invalid User') 之一) ) - 它会被 else ($sender) 条件捕获。因此,我决定在验证条件末尾添加回 die() ,瞧,它按照应有的方式重定向到 if 行内的标题(显示我列出的获取错误)。所以我尝试将骰子添加到函数的末尾 - 不行,它不会被条件捕获。我完全被难住了。有人看到我没看到的东西吗?我什至绝望地添加了大括号,尽管我知道一行条件不需要它们 - 仍然不行。感谢您的任何帮助。

I can't believe I've stared at the following code as long as I have:

public function send($boxMemUsed, $boxMemAllocate) {
    $this->timeSent = gmmktime();
    if ($boxMemUsed >= $boxMemAllocate)
        header("Location: ".HOME_PAGE.PM_PAGE."?view=inbox&status=Memory full - please delete old messages");
    $validation = $this->_validateMessage();
    if ($validation == 'Invalid User')
        header("Location: ".HOME_PAGE.PM_PAGE."?view=compose&status=Errors&error=".$validation);

---->>> if I add die() right here, it redirects to the header above as if the if statement works correctly

    else
        $sender = $this->_database->sendPMessage(get_object_vars($this));
    if ($sender)
        header("Location: ".HOME_PAGE.PM_PAGE."?view=sentbox&status=Message Sent!");
    else
        header("Location: ".HOME_PAGE.PM_PAGE."?view=compose&status=Errors&error=Send Error");
    }

If I add die here, before the end function curly, it catches in the if($sender) conditional.

Simple function in a private message program I'm writing. Okay, so, I go to the live page, no errors, I type in an incorrect username and it is supposed to be caught by $this->_validateMessage() call - it is, I var_dump'd the result directly following and die()'d. Then I checked if the if conditional directly afterward was catching it correctly, it was - and it redirected me...?? But then I remove the testing var_dumps and the die at the end of the if and it doesn't get caught by the same if conditional (the if we're talking about is the if ($validation == 'Invalid User') one) - it instead gets caught by the else ($sender) conditional. So I decide to JUST add back the die() at the end of the validation conditional and voila, it redirects to the header within the if line just as it should (display the get errors I list). So I try adding the die to the end of the function - no go, it doesn't get caught by the conditional. I'm thoroughly stumped. Does anyone see anything I haven't? I even got desperate and added curly braces even though I know they aren't needed on one line conditionals - still no go. Thanks for any help.

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

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

发布评论

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

评论(2

忘年祭陌 2024-11-07 06:25:11

当您调用 header('Location: ...') 时,即使客户端遵循重定向,页面上的其余代码也会继续在服务器上执行。如果这种行为是不可取的(通常情况下也是如此),请在每次调用 header('Location: ...') 后调用 exit;。我怀疑这样做会使您的脚本按照您的预期运行。

When you make a call to header('Location: ...'), the rest of the code on the page continues to execute on the server even after the client has followed the redirect. If this behavior is not desirable (and it usually isn't), place a call to exit; after each call to header('Location: ...'). I suspect that doing so will make your script behave as you expect.

白鸥掠海 2024-11-07 06:25:11

我总是在使用重定向标头或退出后放置一个 die();; Asaph 推荐的也可以。还要确保添加花括号,并为任何空格添加 %20 或使用 urlencode($sting)

if ($boxMemUsed >= $boxMemAllocate) {
    header("Location: ".HOME_PAGE.PM_PAGE."?view=inbox&status=".urlencode("Memory full - please delete old messages"));
    die();
}

我喜欢使用 die 的原因之一是因为我可以打印错误消息以进行调试:

die('There was an error on line number: '.__LINE__);

I always place a die(); after using a redirect header or an exit; as Asaph recommend would work as well. Also make sure to add curly braces, and %20 for any spaces or use urlencode($sting)

if ($boxMemUsed >= $boxMemAllocate) {
    header("Location: ".HOME_PAGE.PM_PAGE."?view=inbox&status=".urlencode("Memory full - please delete old messages"));
    die();
}

One of the reasons I like using die is because I can print an error message for debugging purposes:

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