Jump to content

Recommended Posts

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

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

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.

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:8) 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>

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>

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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