Jump to content

header( ) method for redirect.


williamZanelli

Recommended Posts

Hi guys,

 

I want to redirect users if a certain condition is true, see code below

 

if ($var== 'var_x'){

header("Location: http://www.mywebsite.com/page1.html");
}
elseif ($var== 'var1' || $var== 'var3'){
header("Location: http://www.mywebsite.com/page2.html");
} 

elseif ($surface == 'var4'){
header("Location: http://www.mywebsite.com/page3.html");
}

else{

echo("An Error Occurred. Please email website /admin for.");
}

 

Is this approach correct??

 

My code seems to be throwing an error.. any ideas why?

 

Thanks in advance

 

Will

Link to comment
Share on other sites

Thanks for the reply ctyler86

 

I just rechecked the code.. it works now! AND some thing is being printed before i get into the if statements posted above.

 

Whats the reasoning behind using exit();

 

Thnaks for the prompt reply

 

William.

 

If you're outputting something to the screen but still able to use the header() function without errors, you'll have output buffering enabled.

 

As for why you should exit; well, if you're changing location then you don't want the rest of the script executed do you?

Link to comment
Share on other sites

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.