开发者

Simple conditionals - not working as intended - works with die() at the end?

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.


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.


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__);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜