We help IT Professionals succeed at work.

Notice: Undefined index: HTTP_REFERER

jezella asked
I'm working on some php tutorials where my local testing server appears to work without problems.  However, when I try the following:-

 <?php $url = parse_url ($_SERVER['HTTP_REFERER'])  ?>
 <?php echo $url ?>

I get the following error message.

Notice: Undefined index: HTTP_REFERER in C:\phpdw\untitled14.php on line 10

Basically, I am trying to check from which php page I have come from so that I can create a link back to it.  For example http://phpdw/blog.php?pageNum_getArticles=1&totalRows_getArticles=4

Part of the tutorial is as follows

    <p id="backlink"><a href="<?php
      // find if visitor was referred from a different domain
      $url = ($_SERVER['HTTP_REFERER']);
      if ($url == $_SERVER['HTTP_HOST']) {
        // if same domain, use referring URL
        echo $_SERVER['HTTP_REFERER'];
      else {
        // otherwise, send to main page
        echo 'blog.php';
        } ?>">Back to the blog</a></p>

And this I can't get to work either.

Please help me to understand what I've done wrong.  I'm stuck.

Watch Question

Hi jezella,

From php.net:
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

What browser (user-agent) are you using?

The code looks clean, I don't know why it wouldn't work.  You can check to see if it isset:
if (isset($_SERVER['HTTP_REFERER']))
 // it was set

Joe P



If I click the back link I am directed to as follows

Looks like you are missing semi-colons after those opening two lines.
Should be:

<?php $url = parse_url ($_SERVER['HTTP_REFERER']); ?>
<?php echo $url; ?>

If you have any lines above this code, you might check them for the same missing semi-colons.
Because your web browser or a firewall must be blocking the HTTP_REFERER header from being sent.

In short if you dont care and want to fix this quickly just append an @ symbol onto:-

 <?php $url = parse_url ($_SERVER['HTTP_REFERER'])  ?>


 <?php $url = @parse_url ($_SERVER['HTTP_REFERER'])  ?>

however, the correct way would be to check it is present like described above by BogoJoker

To answer your question a bit better, why not use sessions then set the page name of each page when they visit that in a session variable, an example



$_SESSION['last_url'] = 'contact.php';



   echo 'You previously visited: ' . $_SESSION['last_url'];
If no other suggestions are working, try this:


To see what all gets returned.  At least then you can see if a HTTP_REFERER is even being passed.
What does that @ symbol do JB?

Joe P
Disables error output for that function call.


Ok thank you. After many hours the experts have helped me out.  It's my firewall without a doubt as I tried it with the firewall switched off and all works well.  As to sessions. I'm plodding through the lessons at the moment so need a little, or a lot more time.

I shall go through the above suggestions a bit more so that I get a better understanding.


the @ symbol silences all error messages produced by the function, can also be used on other things, eg.

Thanks JB, really wondered about that. =)

Joe P


JB Thanks a million, I've spent hours on this problem.  Pity a point was not made in the book about possible problems with the firewall.

Keep in mind that just because you fixed the problem by disabling your firewall, visitors to your site may still have the same problem.  Because of that, you should really add the some of the other checks and measures brought up on this page.


Thanks for that Tom, yes I realize this and will have to find a workaround.