RAH Posted September 8, 2007 Share Posted September 8, 2007 Hi, I am using the following code on a contact form: if(!$validation){ // Redirect to page code needs to go here } I have tried multiple methods but due to the headers being sent on this contact form I get the usual "headers already sent" error. How do I get around this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/ Share on other sites More sharing options...
Fadion Posted September 8, 2007 Share Posted September 8, 2007 try meta refresh Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344235 Share on other sites More sharing options...
RAH Posted September 8, 2007 Author Share Posted September 8, 2007 I tried the following however it seems to cause a loop as it's used later on in the script: printf("<meta http-equiv=refresh content=\"0; url=$url2\">"); Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344236 Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 "I have tried multiple methods but due to the headers being sent on this contact form I get the usual "headers already sent" error." Don't send any output to the browser before your header("location: someurl.html"); Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344250 Share on other sites More sharing options...
RAH Posted September 8, 2007 Author Share Posted September 8, 2007 Hi, OK, I'm now using the following at the end of the script: $url = "http://www.yahoo.com"; $url2 = "http://www.google.com"; if(!$validation){ printf("<meta http-equiv=refresh content=\"0; url=$url2\">"); }else{ printf("<meta http-equiv=refresh content=\"0; url=$url\">"); }} If validation is true then it should redirect to yahoo.com. If validation fails it should redirect to google.com. If validation is true then it works however if validation fails it doesn't redirect. Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344289 Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 do you mean to have that extra curly bracket at the end? personally, i don't let the browser control anything if i can help it. that means i don't send a META tag and then assume the browser is going to redirect. imo, you're better off putting a header() redirect in; send the page to the browser, not the browser to the page. Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344363 Share on other sites More sharing options...
RAH Posted September 8, 2007 Author Share Posted September 8, 2007 I've tried a header() redirect and it gives the same "headers already sent" error. Is there some redirect function in PHP which overcomes this issue? Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344396 Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 yes, use header("location: $url") before anything is output to the browser. If there is anything before the first php bracket <?, then that is included in "anything output to the browser". Any echo's, anything at all output to the browser before a header() is invalid. Perform all of your logic before you have to decide whether and where to send the user. Then use header(), or not if you want to continue processing the page. Also, don't forget to put an exit; after any header()'s. Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344398 Share on other sites More sharing options...
RAH Posted September 8, 2007 Author Share Posted September 8, 2007 Hi, The code in it's current state doesn't redirect the user if validation fails for some reason. And if validation is true then the headers already sent error is present. <body><?php $toaddr = "test@test.com"; $subject = "subject here"; $realname = $_POST['realname']; $email = $_POST['email']; $address = $_POST['address']; $date = $_POST['date']; $time = $_POST['time']; $telno = $_POST['telno']; $details = $_POST['details']; $validation = true; $realname = $_POST['realname']; if($realname == ""){ $validation = false; } $email = $_POST['email']; if($email == ""){ $validation = false; } $address = $_POST['address']; if($address == ""){ $validation = false; } $date = $_POST['date']; if($date == ""){ $validation = false; } $time = $_POST['time']; if($time == ""){ $validation = false; } $telno = $_POST['telno']; if($telno == ""){ $validation = false; } $details = $_POST['details']; if($details == ""){ $validation = false; } if(!$validation){ echo "Error: Please ensure all fields are entered."; }else{ mail(Email details here) ?> <?php $url = "http://www.yahoo.com"; $url2 = "http://www.google.com"; if(!$validation){ header($url2); exit; }else{ header($url1) ; exit; }} ?></body> Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344460 Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 you get headers already sent because of the <BODY> tag. That tag gets sent to the browser before the PHP code even starts. The headers are sent BEFORE ANYTHING, including the <BODY> tag. Once you put any output whatsoever before header() it's too late to use header(). Remove the <BODY> tags. Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344466 Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 also don't forget location in your headers: header("location: $url1"); Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344474 Share on other sites More sharing options...
RAH Posted September 8, 2007 Author Share Posted September 8, 2007 Hi, I removed the body tags and updated the code as follows: $url = "http://www.yahoo.com"; $url2 = "http://www.google.com"; if(!$validation){ header("location: $url2"); exit; }else{ header("location: $url1") ; exit; }} Both issues still remain (headers already sent error if validation is true and no redirect if validation is false). Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344480 Share on other sites More sharing options...
BlueSkyIS Posted September 8, 2007 Share Posted September 8, 2007 what happens before the code you are showing us? the code you're displaying shouldn't even compile with the extra bracket at the end. Where are the <?php and ?> tags? Quote Link to comment https://forums.phpfreaks.com/topic/68475-solved-redirect/#findComment-344482 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.