简单的条件 - 未按预期工作 - 最后与 die() 一起使用吗?
我不敢相信我竟然一直盯着下面的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
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 toexit;
after each call toheader('Location: ...')
. I suspect that doing so will make your script behave as you expect.我总是在使用重定向标头或退出后放置一个
die();
; Asaph 推荐的也可以。还要确保添加花括号,并为任何空格添加 %20 或使用 urlencode($sting)我喜欢使用 die 的原因之一是因为我可以打印错误消息以进行调试:
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)One of the reasons I like using die is because I can print an error message for debugging purposes: