I've searched this and other forums to find a solution but as of yet have had no luck.
What I am trying to do is this...
I have a contact form on a page that can be called from various places in the website.
I want to be able to check when someone comes into the contact script, where they came from and set that value to a session variable, so while they are in that contact process, whether filling out the form or correcting entry errors or whatever, when they have finished and submitted the contact form successfully, they will be presented with a button that will take them back to the page they came from (where they clicked on the contact me link).
Seems it should be pretty simple to check upon entry to the contact.php, whether the $back variable has been set yet, and if not, then set it as well as the session variable by the same name.
THEN
When the page reloads to correct data entry errors, or to display the successful submission notice with the return link, that session variable should contain the original referer page so you can use it in a link.
BUT
What happens is that I get the last referer page, which is always the contact.php, which does me no good in getting back to where they came from.
Here is what I have in a shorthand version
// script contact.php
<?
if (isset($session['back'])){
$back=$session['back'];
}
if($_POST) { // if the form below has been filled out and submitted
/// process the posted data for errors
if (!isset($error_vars)) { // no errors in entry data
/// send contact form
$goback=$back;
unset ($back); // clears these variables for next time if they click the contact link again
unset ($_session['back'];
?>
<a href="<?echo $goback;?>">Continue From Where You Were</a>
<?
exit; // won’t continue past this point
}
}
// if nothing posted or if errors in entry data, will fall through and display the form
// along with entry errors displayed
// if $back hasn't been set, then this is the first time here
// and the http_referrer should contain the originating page
if(!isset($back)){ // only set these if they haven’t been set before
$back=$SERVER['HTTP_REFERER'];
$session['back']=$back;
}
// in testing, I can echo the $back variable upon entry, and it contains the correct url
?>
Display html form for entry and submission
RESULTS of running this are always the same : the $goback variable is always set to the contact.php url, as opposed to one of the various website pages that should be in the link.
I'm sure it is something simple that I am doing wrong, but for some reason, I just can't see it.
Any help that you may have would be greatly appreciated.
Thanks
Doug