Recipe 9.2. Adding the Referring Page to a Form


Problem

You need to know the address of the last page a visitor was viewing before they contacted you through your web site.

Solution

Set the value of a hidden form field to the referring page address using the server-side include, or PHP tag for accessing this variable.


Using SSI:

<!--#echo var="HTTP_REFERER" -->


Using PHP:

<? echo $HTTP_REFERER; ?>

Discussion

When you solicit feedback from your web site through a contact form, one thing you'll get only once in a blue moon is the web page a visitor is writing to you about. Even if you explicitly ask people to include the URL of the problem page, you're much more likely to get cryptic messages like "There's a broken link on your site."

Fortunately, the pages on your site know the address of the page that contained the link that got them to your contact form. By embedding a simple environment variable in a hidden field, you'll stand a better chance of finding the problem a user has reported.

Using server-side includes, construct your hidden field like this:

 <input type="text" size="100" maxlength="100" name="referrer"         value="<!--#echo var="HTTP_REFERER" -->"> 

In the code of your form, the variable will appear as a complete URL: http://domain.com/path/to/webpage.html.

Both Apache and PHP spell "referrer" without the double "r" in the middle of the word.


If you're not using SSIs or PHP on your web site, you also can use the referrer property (note the double "r") of JavaScript's document object, but you have to wrap the whole form field tag in a document.write script, like this:

 <script>document.write('<input type="hidden" name="referrer"            value="'+document.referrer+'">');</script> 

Another useful environment variable is the HTTP_USER_AGENT, which has the same name in both Apache and PHP. The user agent variable will tell you the browser and operating system of the respondent's computer, which can be critical information when hunting down site bugs and replicating user errors. The variable prints a long string of information that looks like this:

 Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1   (KHTML, like Gecko) Safari/312 

The code to embed the user agent in a hidden field using PHP looks like this:

 <input type="hidden" name="agent" value="<? echo $HTTP_USER_AGENT; ?>"> 

See Also

Recipe 9.1 explains how to redirect users from missing or relocated pages; you may want to pair that with this Recipe to find out where they came from. Recipe 1.4 explains how to setup SSIs, and Recipe 4.4 explains how PHP and other scripts can be inserted into your pages.

Recipe 9.10 discusses ways to find and fix problems users encounter while visiting your web site.



Web Site Cookbook.
Web Site Cookbook: Solutions & Examples for Building and Administering Your Web Site (Cookbooks (OReilly))
ISBN: 0596101090
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Doug Addison

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net