Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

referrer URL in form?



7:54 pm on May 23, 2007 (gmt 0)

5+ Year Member

Hello everyone!
I am in desperate need of some help on an issue I'm having with a site I'm building. Basically, I'd like to know if it's possible to get my form to submit the referring URL (previous page info) or something like that. I'll explain:

I'm designing a site for a travel company. They already have a booking engine but they want a section on the site where they could post in-house products (each product on it's own full page). There is NO CMS for this, I'm building them in plain HTML. So, if a client is interested in one of the packages, they would click the "Reserve now" button and are taken to the "reservation" form. My problem is that I don't want to build an individual form for each page, I'd rather have one central form that somehow catches or records the referring URL (or package info) so that when the form submits, we will know which package they want to reserve.

Can anyone help me?


9:42 am on May 24, 2007 (gmt 0)

5+ Year Member

Using PHP you can use the $_SERVER['HTTP_REFERER'] variable, however I believe that cetain web servers don't supply this. I would suggest that any web servers worth their salt will supply this variable.


9:44 am on May 24, 2007 (gmt 0)

5+ Year Member

Sorry, Just read the second Para, don't think this is possible without PHP, don't know of any HTML or Javascript that will give you this info.


11:10 am on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

I thought the location object had the referer but just looked it up and it hasn't. The only way (easiest) to do this would be to use that tiny bit of PHP.

I wrote a web stats package which uses the referer and most of the time it works, however sometimes it's blank. Doesn't seem to matter what OS or what Browser, but I think some firewalls can block it. It's about 96% reliable I think.


12:56 pm on May 24, 2007 (gmt 0)

5+ Year Member

The browser I know that doesn't is Abyss, Apache does and IIS does.


1:22 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

You mean servers. With my webstats software I made it record HTTP_USER_AGENT if there was no referer string, absolutely no correlation.

Sometimes the same user (can tell from cookie ID) would appear with no referrer, then it would magically appear after they'd looked at a few pages.


6:35 pm on May 24, 2007 (gmt 0)

5+ Year Member

Hi guys, thanks for getting back to me!

If I were to do this with PHP, would that work WITHOUT me having to change my HTML pages? I just want the form to submit a hidden field which records the referrer URL (previous page they were on). Can a PHP form do that? And if so, does anyone know of any ready-made scripts available (sorry,I'm not a coder - just a designer) =)


7:55 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Yes, PHP is inline with your HTML which means, assuming your server supports PHP, that you can just add 1 line into your form page.

I don't do PHP either though, colandy can you provide the complete code snippet?


7:57 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Let me have a go, see if I've learned anything from this forum.

I think this is what would go into your form page.....

<input type='hidden' name='referrer' value='<?php print $_SERVER['HTTP_REFERER']?>'>

How did I do?


8:43 pm on May 24, 2007 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Unless I'm missing something, why don't you just include an identifying paramater in the link for the Reserve now" button that identifies which page you're coming from? Then just pass that paramater in the form.

Also, take it from experience- you/they will definitely want some sort of CMS developed. Travel products (or at least some of the related information) change over time, and hand editing HTML files is not productive.


9:04 pm on May 24, 2007 (gmt 0)

10+ Year Member

I also use the same method as LifeinAsia is proposing for my mailform. On each page there is a link to the mail form and one of the parameters is the URL of the referring page. This involves however some editing of the HTML files. One can not rely on the $_SERVER['HTTP_REFERER'] variable to be correct.
So for each link in the HTML files to the reservation form make change it to something like:

<a href="reserve.php?ref=http://www.example.com/product1.html>Book Now</a>


9:12 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

if your intention is to pre-populate the reservation form with whatever it is they want to book, then you could put all of the information down as parameters in the URL. (presuming that none of it is secret)

for example, make the link to the reservation form something like this



5:41 pm on May 25, 2007 (gmt 0)

10+ Year Member

Bellabia, check your stickymail - I think you'll find that's what you're after? May need adapting very slightly.


9:23 am on May 30, 2007 (gmt 0)

5+ Year Member


<input type='hidden' name='referrer' value='<?php print $_SERVER['HTTP_REFERER']?>'>

Was pretty close, I'd change the 'print' statement to an 'echo' statement though.

Just personal preference I think.


9:34 am on May 30, 2007 (gmt 0)

5+ Year Member

i would have done
<input type='hidden' name='referrer' value='<?=$_SERVER['HTTP_REFERER']?>'>

technically speaking you could do this with JS and .htaccess .PHP or another server side language would be better though


1:19 pm on Jun 5, 2007 (gmt 0)

10+ Year Member

I've been looking for this as well.

In my case the form is on the homepage, which is an html file. If I would put some php code in it, I'd have to change the file extension and lose all search engine rankings!

Is this really (sending referring URL in a form) not possible using Javascript?

I'm using the following Javascript code, but it doesn't seem to work:

<!-- this goes in the page header -->
function start() {
var ref = document.getElementById('httpref');
ref.value = document.referrer;
onload = start;

And this line in the form:

<input type="hidden" id="httpref" value="">

Is there something wrong with the code?

Thanks in advance.


1:40 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

No, there's something right with your code.

ref.value = document.referer;

Unfortunately, whoever wrote the book on this couldn't spell, so only 1 'r' in referrer.


Featured Threads

Hot Threads This Week

Hot Threads This Month