This 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?
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!
When headering to the previous page in PHP you can’t rely on HTTP_REFERER being set!