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

    Towards the end of your posted code
    shouldnt the test be isset(SESSION-back)

    if(!isset($_SESSION['back'])){ // only set these if they haven’t been set before

    At least you can make a try, and see what happens.

    I am sure you have session_start() somewhere, too

    <?php
    session_start();
    
    // and the http_referrer should contain the originating page
    if(!isset($_SESSION['back'])){    // only set these if they haven’t been set before
    	$back = $_SERVER['HTTP_REFERER'];
    	$_SESSION['back'] = $back;
    }

      When they first arrive at the contact page:

      <?php
      if(!isset($_SESSION['back'])) {
           $_SESSION['back'] = $_SERVER['HTTP_REFERER']
      }
      ?>
      

      Will set the session variable to where they came from, and not change it when the form is submitted to itself. Then after the user completes it and the validation routine succeeds, you can set a variable from the Session variable, unset the session variable, and use the variable to redirect the via the "Go Back to Where I Came From" button.

      <?php
      if(isset($_POST['submit']) && empty($errors)) {
           //Send the form data to database/by email/whatever . . .  
      $back = $_SESSION['back']; unset($_SESSION['back']; } ?> Thanks, man! Your message was sent successfully, blah, blah, blah. <a href="<?php echo( $back ); ?>">Go back to where I was</a>

        Have a look at my version of your nice idea.

        1. I use not a lot of $back, $goback variables. They can confuse ...
          Only $_SESSION['back']

        2. I test if HTTP_REFERER is set. This is not always the case.
          If not, I put 'index.php' (Home) for the goback page.

        See what you think.
        I got it working as it should this way:

        <?php
        session_start();
        
        if(!isset($_SESSION['back'])){ //only set these if they haven’t been set before
        	$_SESSION['back']=isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER']:'index.php';
        }
        
        if(isset($_POST['submit'])){ // 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
        
        	?>		
        	<a href="<?php echo $_SESSION['back'] ?>">Continue From Where You Were</a>
        	<?php
        	unset($_SESSION['back']);//clears for next time if they click contact link again
        	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
        
        // in testing, I can echo the $back variable upon entry, and it contains the correct url
        
        ?>
        <form method="post">
        <input type="submit" name="submit">
        </form>
          halojoy;10945928 wrote:

          Have a look at my version of your nice idea.

          1. I use not a lot of $back, $goback variables. They can confuse ...
            Only $_SESSION['back']

          2. I test if HTTP_REFERER is set. This is not always the case.
            If not, I put 'index.php' (Home) for the goback page.

          See what you think.
          I got it working as it should this way:

          <?php
          session_start();
          
          if(!isset($_SESSION['back'])){ //only set these if they haven’t been set before
          	$_SESSION['back']=isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER']:'index.php';
          }
          
          if(isset($_POST['submit'])){ // 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
          
          	?>		
          	<a href="<?php echo $_SESSION['back'] ?>">Continue From Where You Were</a>
          	<?php
          	unset($_SESSION['back']);//clears for next time if they click contact link again
          	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
          
          // in testing, I can echo the $back variable upon entry, and it contains the correct url
          
          ?>
          <form method="post">
          <input type="submit" name="submit">
          </form>

          Yes, that makes more sense than the way I had it. When I started writing mine, I had it in my mind that I'd use a header() redirect, so I wanted to unset the session var before redirection . . .

            Thank you to both of you, halojoy and Pikachu2000.

            That was EXACTLY the solution that I needed halojoy, I plugged in the changes from your final post, and that was it.
            ...

            Works perfectly every time (to the degree that I've tested anyway).

              Write a Reply...