Jump to content

HTTP_REFERER undefined


parka

Recommended Posts

Hi all,

 

I'm trying to print a referral link  on my page, the code is as follows:

 

$refererSite = $_SERVER['HTTP_REFERER'];
if ($refererSite !== null){
print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n");
}

 

When I put it onto a page, it'll get the Undefined Index error. It does so if a user were to have the page listed as homepage and was not directed there from other pages.

 

What's the recommended way to getting around this?

 

Should I suppress the error by adding "@"?

 

Link to comment
https://forums.phpfreaks.com/topic/40164-http_referer-undefined/
Share on other sites

Try this...

<?php
  $refererSite=$_SERVER['HTTP_REFERER'];
  if (!empty($refererSite)) {
   print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n");
  }

Note that if you enter the URL that script is stored on your PC directly and not linking to it from another page then the referer will be empty - I got caught out by that!

 

Oh, and as magnetica says :D

Try this...

<?php
  $refererSite=$_SERVER['HTTP_REFERER'];
  if (!empty($refererSite)) {
    print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n");
  }

Note that if you enter the URL that script is stored on your PC directly and not linking to it from another page then the referer will be empty - I got caught out by that!

 

Oh, and as magnetica says :D

 

Thanks.

 

If it's empty, I can have a "else" print nothing, right?

This will work:

<?php
  $refererSite=$_SERVER['HTTP_REFERER'];
  if (!empty($refererSite)) {
    print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n");
  } else {}

But I don't know why you'd need an empty else.

 

Good point. Just realised that.

 

Anyway, I still have the undefined index caused by $_SERVER['HTTP_REFERER'].

Should I just suppress it with "@".

Is it good programming practice?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.