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
https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/
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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.