odintheterrible Posted January 14, 2010 Share Posted January 14, 2010 Greetings, As a new comer to php, please accept my apologies if this has been done to death, so to speak, but I've tried 'Googling' it and various other sources without any luck. I simply want to redirect the browser to go back to the previous (referring) page after briefly displaying a page with an error message on it (failed form fill in for instance) ... sounds simple enough ... I was given this code : <?php $ref = $_SERVER['HTTP_REFERER']; header( 'refresh: 4; url='.$ref); ?> ... trouble is it returns an error ... "Warning: Cannot modify header information - headers already sent by (output started at /mounted-storage/home122b/sc11574-ANOC/****.**.**/sorry.php:9) in /mounted-storage/home122b/sc11574-ANOC/****.**.**/sorry.php on line 44" ... and does not return to the referring page. Can one or other of you informative guys or gals throw a little light into my world and solve this please ?? Many thanks in anticipation. O T T Quote Link to comment https://forums.phpfreaks.com/topic/188510-problem-redirecting-browser-to-previous-referring-page/ Share on other sites More sharing options...
mikesta707 Posted January 14, 2010 Share Posted January 14, 2010 that header error happens when output (including any whitespace or html tags) happens before that line. IE echo "hi"; header("Location: somewhere.php"); would throw the same error. To be of more help we would have to see the code in question, but make sure you don't have any whitespace, html tags, or other output (print, echo, etc) before that line. Based on the error message, particular this part headers already sent by (output started at /mounted-storage/home122b/sc11574-ANOC/****.**.**/sorry.php:9) it appears the output is sent on line 9 (you can tell which line and file the output starts by from this string.) so check that line Quote Link to comment https://forums.phpfreaks.com/topic/188510-problem-redirecting-browser-to-previous-referring-page/#findComment-995225 Share on other sites More sharing options...
oni-kun Posted January 14, 2010 Share Posted January 14, 2010 And just remember that HTTP referrers are not to be relied upon, as they are sent by the client and often left out, so it'd be wise to leave a ' if you are not redirected automatically, please press back on your browser' message. Quote Link to comment https://forums.phpfreaks.com/topic/188510-problem-redirecting-browser-to-previous-referring-page/#findComment-995238 Share on other sites More sharing options...
odintheterrible Posted January 15, 2010 Author Share Posted January 15, 2010 that header error happens when output (including any whitespace or html tags) happens before that line. IE echo "hi"; header("Location: somewhere.php"); would throw the same error. To be of more help we would have to see the code in question, but make sure you don't have any whitespace, html tags, or other output (print, echo, etc) before that line. Based on the error message, particular this part headers already sent by (output started at /mounted-storage/home122b/sc11574-ANOC/****.**.**/sorry.php:9) it appears the output is sent on line 9 (you can tell which line and file the output starts by from this string.) so check that line THanks for that swift response ... but it still leaves me baffled. I put the code into a new page with nothing else to test it and it still returns an error ... Warning: Cannot modify header information - headers already sent by (output started at /mounted-storage/home122b/sc11574-ANOC/***.**.uk/sorry.php: in /mounted-storage/home122b/sc11574-ANOC/***.**.uk/sorry.php on line 10 ... so as requested, here is the full code from the new page : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $ref=$_SERVER['HTTP_REFERER']; header('refresh:4;url='.$ref); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/188510-problem-redirecting-browser-to-previous-referring-page/#findComment-995267 Share on other sites More sharing options...
oni-kun Posted January 15, 2010 Share Posted January 15, 2010 And there's your problem. You cannot output anything before a header() is called. As sending a header after the content is already pushed is invalid. This should work provided nothing else is being included before the <?php. <?php $ref=$_SERVER['HTTP_REFERER']; header('refresh:4;url='.$ref); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/188510-problem-redirecting-browser-to-previous-referring-page/#findComment-995269 Share on other sites More sharing options...
odintheterrible Posted January 15, 2010 Author Share Posted January 15, 2010 Greetings, Many thanks for that !! I wasn't aware that you could put anything in front of the HTML tags in the header and for it to still be valid. Without pushing my luck too far though ... I've instigated the php into a full web page which is basically a 'sorry' page for an incomplete form ... that is, if the required fields aren't filled in, then this 'sorry' page displays for 10 seconds and then returns to the form for the person to try again. I've added a 'return' button to the bottom of the page just in case the person doesn't want to wait for the 10 secs. It all works OK ... but ... pressing the return button (form field) returns the viewer to the original form page OK and still displays the data that was entered into the form so it's easy to simply make the alteration/addition and then resubmit the form. The remaining problem with the php bit of the code is that it returns the viewer to the form ... but the submitted data is removed. Is it possible to alter the php code to return to the referring form page WITH the original data still visible ??? If so, how would I achieve this ??? Many thanks once again. Quote Link to comment https://forums.phpfreaks.com/topic/188510-problem-redirecting-browser-to-previous-referring-page/#findComment-995442 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.