williamZanelli Posted July 18, 2008 Share Posted July 18, 2008 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 Quote Link to comment Share on other sites More sharing options...
ctyler86 Posted July 18, 2008 Share Posted July 18, 2008 You need to make sure nothing is being printed out to the page before your header call, and also it might be good to add an exit() call after each header() call header("Location: http://www.google.com"); exit(); Quote Link to comment Share on other sites More sharing options...
williamZanelli Posted July 18, 2008 Author Share Posted July 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 18, 2008 Share Posted July 18, 2008 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? Quote Link to comment Share on other sites More sharing options...
ctyler86 Posted July 18, 2008 Share Posted July 18, 2008 Durp, thanks for the answer- wouldn't let me post mine. Quote Link to comment Share on other sites More sharing options...
williamZanelli Posted July 18, 2008 Author Share Posted July 18, 2008 Cheers guys. Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 18, 2008 Share Posted July 18, 2008 probably more efficient to change this header("Location: http://www.google.com"); exit(); to exit( header( "Location: http://www.google.com" ) ); [code] But that's just me... [/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.