HTTP_REFERER in PHP

Posted: June 23rd, 2009 | Author: | Filed under: (X)HTML, JS, PHP | Tags: , , , | Comments Off on HTTP_REFERER in PHP

linkingThis morning I added some new features to a webapplication we are developing. They worked fine in Firefox, so I opened Internet Explorer to see if everything worked in there too. Of course it didn’t…

In webapplications you may need to know which webpage requested the current one. PHP offers an easy way to find the URL of that page: the predefined server variable $_SERVER[‘HTTP_REFERER’]. Unfortunately this variable was not set in IE. So where did it go?

Linking

While searching for the cause of my problem I noticed that the page on which the error showed was requested through JavasScript, using code similar to:

<input type="checkbox" onclick="window.location='http://myhomepage.com/';" />Check my homepage!

I made a simple test page to see if what I suspected was correct:

HTTP_REFERER: <?php echo $_SERVER['HTTP_REFERER']; ?><br />
<a href="referer.php">Refresh page!</a><br/>
<input type="checkbox" onclick="window.location='referer.php';"/>Check the refreshed page!

Results were as expected: while the HTTP_REFERER was always set correctly in Firefox, it was empty when clicking on the JavaScript link in IE.

Conclusion

When headering to the previous page in PHP you can’t rely on HTTP_REFERER being set!


Comments are closed.