How to Discover the HTTP Referer

Using ASP, JavaScript, PHP, or the Web

There are many HTTP headers you can use in your web pages, but one of the more interesting ones is the HTTP Referer (yes, that’s how referer is spelled). This tells you where the reader came from before they arrived at this page.

As a web designer, if you have information about a web page, you can use this information. And the HTTP Referer is no exception. But in order to use it, you need to be able to access it.

Using JavaScript to Get the Referer

JavaScript is one of the easiest ways to get the referer because it is part of the DOM. You write:

document.referrer

Inside your script. You can set this as a variable or add it to a document.write() function. Remember that sometimes the referer is blank, so it can help to add an if statement around it:

if (document.referrer) {
  document.write(document.referrer);
}

Using PHP to Get the Referer

PHP is another easy way to get the referer. As with JavaScript you can set it as a variable, or add it to an echo statement. You write:

$HTTP_REFERER

Remember to check if it’s empty.

Using ASP to Get the Referer

ASP also has a system variable that you can call to get the referer. Write:

Request.ServerVariables("HTTP_REFERER")

As with JavaScript and PHP, you can then add it to a (non-system) variable or write it to the HTML with Response.Write().

The Web Can Provide Referer Information, Too

These tools are not as useful to web designers because they only tell you the referrer to the page you just came from.

For instance if you click the below links, you should get this article listed as your referrer. One way you can use them is to test links you’ve set up referrer blocking on.

Add one of the links below to your web page, and then add your referrer blocking to the page. If the referrer shows up as blank or <<null>> then it’s working.

Otherwise it isn’t. Once you’re confident your scripts are working, you can delete the link.

You can also use these links to test anonymizing and referrer spoofing programs and scripts you’ve installed on your browser. In that situation, you just visit one of those pages with your software running, and the page will show you if it’s working.

Get more information about the HTML referer: What is a Referer | Why is Referrer Spelled "Referer" in Web Devlopment? | What is My Referer | Test Your Browser for the Referer | How to Use the Referer on Web Pages | How and Why to Hide Referer Information | What is Referer Spam | How to Block Referer Spam | What is Referer Spoofing? | What is the rel=noreferrer Attribute | What is the DOM referrer Object