Articles
HTTP Headers And Referrer Tracking
Knowing the referring page to your web site can be pretty helpful. One of the HTTP headers that
can help you with this is the Referer header that a client sends in request.
Here are some things that you can do with the referrer information:
- See who is referring traffic to your site
- See what search engines are referring users to your site
- See what users are searching for
- Force users to enter your site through a specified page
On the client side, using JavaScript to obtain the referrer is pretty simple. Here is a small example:
alert("Referring page: " + document.referrer );
To obtain the referrer on the server side, it's pretty easy as well:
Using VBScript in ASP:
Response.Write Request.ServerVariables("HTTP_REFERER")
Using PHP:
echo $HTTP_REFERER;
Using ColdFusion:
<cfoutput>#cgi.HTTP_REFERER#</cfoutput>
You can see who is refering traffic to your site just by looking at the output from the above code.
For example, the referring site to this page is:
undefined.
As you can see, the referring domain is undefined.
If you saw this referrer: http://www.google.com/search?q=http+headers, you could
conclude that someone searched for "http headers" on google.
If you wanted force users to enter your site through a specific page, you could easily do so with
a server side script. Here is a simple example of an ASP script using JavaScript that checks to
see if the referring page is one that is on your domain. You would include this on every page that
you want to redirect users to a specific page if the referring page is not yours.
var mydomain, referrer, redirect;
mydomain = "httpheaders.com"; // change this to be your domain
redirect = "index.asp"; // the page to redirect to
referrer = String( Request.ServerVariables("HTTP_REFERER") );
if( referrer.indexOf(mydomain) < 0 )
Response.Redirect( redirect );
Be careful. Some web proxies strip out the Referer field before sending the HTTP
request to the origin server. However, most proxies will include the Referer field.
Also, if a user types in a URL, there won't necessarily be a Referer field.
That about covers it. Good luck. There are some links below with more information on tracking user information.
Other tutorials about referrers:
http://hotwired.lycos.com/webmonkey/98/16/index2a.html?tw=e-business
|