Okay so my page is a feedback form for a website. The user enters their feedback (name, comments, etc) into the form. There is a hidden field that is to store the http_referer. Once the user fills in the form, they 'preview' their feedback before they can send it. Problem is, when they preview it, the http_referer is changed to that of the form's original page instead of the site that they got to the form from.

The referer is stored in a Feedback object, and when the preview page is rendered, the form is rendered again underneath the preview, causing a 'new' Feedback object I believe.

Is there a way that I could have some sort of variable that I could use for the entire page (global variable or something?) that I could use to determine whether or not it is the first time the form is rendered (therefore storing and keeping only the first referer)? The main problem is trying to get around the new Feedback object being initialized.

The code:

index.php:

include('Controller.class.php'); ?>

$C = new Controller;

if(!array_key_exists('action', $_REQUEST)) {
	$_REQUEST['action'] = 'form';	
}

$cmd = '$C->' . $_REQUEST['action'] . '();';

eval($cmd);

Controller.class.php:

	function form() {
		$fb = new Feedback;

	if (isset($_REQUEST['http_referer'])){ 

	if ($_REQUEST['http_referer'] == '') {
			$fb->http_referer = $_SERVER['HTTP_REFERER'];
		} else {
			$fb->http_referer = $_REQUEST['http_referer'];
		}
	}

    How about your submit button?

    if (isset($_REQUEST['submit'])) {
      // this is not the first time
      $fb->http_referer = $_REQUEST['http_referer'];
    } else {
      $fb->http_referer = $_SERVER['HTTP_REFERER'];
    }
    

    Also, HTTP_REFERER is set by the client so there are cases where it never gets defined. Not every user agent takes care to specify it.

      hi there, thanks for the response 🙂

      I forgot to include the form code, sorry:

      show_form.php:

      <input type="hidden" name="http_referer" id="http_referer" value="<?= $feedback->http_referer ?>" /> 
      
      <input type="hidden" name="action" value="form" />
      
      <input type="submit" value="Preview" />
      

      i know there are cases where the referer never gets assigned, I wouldn't care about including this info on my own sites. My boss, however, wants to store where the user gets directed to our page from, which gives me the task of figuring out how to keep only the first referer stored in the session to be submitted with the form.

        I'm guessing the problem is not your preview form but the final form. I would be (and you'll need to verify this) that the referrer you see in that first form where the submit button says 'Preview' is usually the right value. When you click preview, you see a 2nd form, right? If that 2nd form doesn't also have an input for http_referer (which contains the value submitted by the Preview submit) then the final result will end up taking http_referer from the server.

        I hope that makese sense. Basically you want to do this:

        First form
        Display form with hidden input named http_referer which takes value from $SERVER
        Clicking submit button (which is labeled 'Preview') takes you to 2nd form.
        2nd form
        Display form with hidden input named http_referer which takes value from $
        REQUEST
        clicking submit button (i have no idea what this says...OK? FINAL?) results in form hander being called.
        form handler
        Because you had a input in the 2nd form, you should now have the origina value for http referer

        I hope that makes sense. It's pretty hard to figure out when you reference all these classes that we haven't seen.

          Okay, sneaky, I think you're really close....I have thought about doing that, I just don't really understand how I would do it since my form and previews are basically just different displays on the same page...does that make sense? The site is here if you want to see it. Maybe you'll understand what I just said better. I have the referer displaying at the top of the form now...you can see it change when you hit preview..I'm not even sure why it changes .... why would the $fb->http_referer be accessed from there?

            Okay so I was able to get the first referer by using an uneditable textfield 🙂

            Put the original referer into the text field by checking if the field was empty...if it was, the referer was put in using the $SERVER call, if not, then the field kept its value using $REQUEST. Then I made it uneditable so the user couldn't put something other than the referer into the field 🙂

            Out of the box kind of way to do things I guess, but it works!

            Thanks for your help Sneakyimp, helped me think this up 🙂

              This code says "I don't give a damn what $POST['http_referer'] is or what $GET['http_referer' is, if there is a value for $SERVER['HTTP_REFERER'] then use that please." I'm guessing you don't want to do that.

                             if (isset($_SERVER['HTTP_REFERER'])){
                          $fb->http_referer = $_SERVER['HTTP_REFERER'];
                      } 
              

              You should try to set up some kind of trace/logging function...or perhaps just echo debug messages right out to the screen. That will give you a much better idea of the logic flow in your site.

                I tried going to your site but I got 'not found' for that link you sent.

                  sorry haha I deleted that page that's supposed to link to the form so it will get a referer.

                  I got the site working anyhow...new code:

                  if (isset($_REQUEST['http_referer'])){ 
                  	$fb->http_referer = $_REQUEST['http_referer'];
                  
                  } elseif (isset($_SERVER['HTTP_REFERER'])) {
                  	$fb->http_referer = $_SERVER['HTTP_REFERER'];
                  }
                  

                  which is basically what I had to start with, and is what you suggested i believe...

                  Just used a textbox instead of the hidden field and it worked for some reason.

                    Write a Reply...