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 Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/ 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(); Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/#findComment-593632 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. Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/#findComment-593634 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? Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/#findComment-593638 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. Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/#findComment-593639 Share on other sites More sharing options...
williamZanelli Posted July 18, 2008 Author Share Posted July 18, 2008 Cheers guys. Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/#findComment-593651 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] Link to comment https://forums.phpfreaks.com/topic/115475-header-method-for-redirect/#findComment-593654 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.