HI there,
I am some problems creating a redirect from a php file.
Now, here's teh problem. On my site - let's take this page - http://antobbo.webspace.virginmedia.com/webediting/content.htm I have a comment form that sends me an email everytime somebody submits the form. Now, the php file is configured so that if I send submit the form from any page of my website the user is then redirected to the home page after 5 seconds.
Here's the php file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>

<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<meta http-equiv="REFRESH" content="5;url=http://www.antobbo.webspace.virginmedia.com/webediting/home.htm">
<link rel="stylesheet" type="text/css" href="style.css" >

<title>Thank you</title>
</head>
<body>
<div id="wrapper">



<!-- BANNER STARTS HERE -->
<div id="banner">
<h1>Web editing - Livelink WCM</h1>
</div>
<div id="text_navigation">
<?php
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];

mail($to,$subject,$message);
echo "<h2>Thank you</h2><p><br><br>Thank you for sending your feedback. That will help me improve the website.<br><br></p>";
?>
</div>
</div>
</body>
</html>

What I am trying to do is to make sure that if you submit the form from page A then the php files redirects you to page A and not to the home page. In the case above if I submit the form from the "Content issues" page I want the redirect to bring me back to that particular page.
I was taking a look around and reading a bit about this. I haven't written any code yet, and I was wondering if the php session would be a good way to do it, or if there is any other way to do it. The thing is I don't know what variable can represent a specific page, to be used, for instance, in the php session...any suggestion?
thanks

Recommended Answers

All 4 Replies

<?php if (isset($_SERVER['HTTP_REFERER'])) { echo "<a href='$_SERVER['HTTP_REFERER']'>back</a>"; } else { echo "<a href='javascript<b></b>:history.go(-1)'>back</a>"; }?>

Will take it back or make the url on the link that submits the form include $_server['PHP_SELF'] as part of the array and redirect to the value passed to the form handler
example

<form action='fred' method='post'>
<input type='hidden' name='ralph' value='<?php echo $_server['PHP_SELF']; ?>'>
<input all the other garbage></form>[/code][code=php]<meta http-equiv="REFRESH" content="5;url=<?php echo $_post['ralph']; ?>">[/code]
//bla bla bla
something $_post['ralph'];

Did not even write the code out something similiar can be done with http_referer but not all browsers send the referer

thanks for that. I don't know much about php sorry so i might need some explanation : - )
I tried your second method but the page doesn't seem to validate (surely it's me doing something silly) but thinking of it I would like to give a go to the first method, but I would like to understand it correctly. Basically it displays a "back link" to the referrer wihch is the page you came from. Although I don't quite understand the structure of it. with the isset() function we check that the array server contains the referrer page: if it does we display "back" that brings me back to the referrer page, if not...ehm...brings me back as well (don't know what

javascript<b></b>:

means...)
Also where should all this go in the php file?
thanks

I dunno what the <b></b> means either, when I typed it in, and viewed the submitted post it said

javascript:history.go(-1)

in php
if isset($_server
checks for the existence of the referer in the server array, as you thought, some browsers do not send the referer , the link goes to the actual page if the page is known, or goes back 1 (change the number for two etc, if the page is unknown
non-validating, the variable $_server is not set until the page is referenced from another page that makes the first choice wrong, and there is no history in the validator so that makes the second choice wrong, on an active page there will (should) be oine or the other,
I prefer the 'ralph' method, ralph and fred becuase I did not know the form name on your submission page, explicitly declaring the refering page in the $_post array, so that it accessible to the later processing file
which works even when you get to generation2 of the website and the questionaire form becomes an include file that you just lump in anywhere you want the questions to appear

Member Avatar for brewbuff

You asked if you should use a session variable and that got me thinking... that might be an easy way to do it. If every page placed its own name in a session variable when it is executed then you would always know which page to redirect back to.

$_SESSION['page'] = "fred.php";

Then to return to that page...

header( "Location: $_SESSION['page']" ) ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.