We help IT Professionals succeed at work.

Notice: Undefined index: HTTP_REFERER

jezella
jezella asked
on
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
Array

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.

Ashley
Comment
Watch Question

Hi jezella,

From php.net:
'HTTP_REFERER'
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

Author

Commented:
PS

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

http://phpdw/<br%20/><b>Notice</b>:%20%20Undefined%20index:%20%20HTTP_REFERER%20in%20<b>C:/phpdw/blog_detail.php</b>%20on%20line%20<b>43</b><br%20/><br%20/><b>Notice</b>:%20%20Undefined%20index:%20%20host%20in%20<b>C:/phpdw/blog_detail.php</b>%20on%20line%20<b>44</b><br%20/>blog.php
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.
Commented:
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'])  ?>


eg.

 <?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



contact.php

session_start();

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






index.php

session_start();

if(isset($_SESSION['last_url']))
{
   echo 'You previously visited: ' . $_SESSION['last_url'];
}
If no other suggestions are working, try this:

print_r($_SERVER);

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.

Author

Commented:
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.

Ashley

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

 echo @$_SERVER['HTTP_REFERER';
Thanks JB, really wondered about that. =)

Joe P

Author

Commented:
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.

Ashley
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.

Author

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

Ashley