Jump to content

Recommended Posts

I don't know if I have been awake too long and im missing something stupidly obvious, but I cannot work out why this script isn't behaving as expected. I have simplified it down as much as I can in my code yet it is still giving me problems:

 

if (1 == 1) {		
header("location:1.php");
} else {
header("location:2.php");
}

// other code down here

 

When running this, I would expect to be relocated to 1.php, however, my "other code" is running instead. The weird part is that if I do this:

 

if (1 == 1) {		
header("location:1.php");
} else {
header("location:2.php");
}

exit();

// other code down here

 

I do get relocated.

 

:shrug:

 

Im sure its really simple, but I am just lost. I have been hopping in between PHP and Coldfusion all day so I might just be getting some basic syntax wrong.. I don't know. Any help would be very much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/208704-neither-if-or-else-code-running/
Share on other sites

The header() function does NOT end the script. It will continue to run.  If you want to redirect immediately, you need to exit the script immediately.

 

if (1 == 1) {
    header("location:1.php");
    exit();
} else {
    header("location:2.php");
    exit();
}

// other code down here

Thankyou David,

 

I've been teaching myself PHP for a little while now, but didn't even realise I was supposed to be doing it like this. I think I must have been quite lucky not to have come across this problem before considering that how I always do my relocates!

 

Thanks again - you learn something every day :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.