user892134
user892134

Reputation: 3214

PHP HTTP_REFERER and anchor tag href

I'm trying to get the referer url on destination website by using $_SERVER['HTTP_REFERER'].

example.com

<a href="example2.com">Click Me</a>
<a href="example2.com/page/2">Click Me</a>
<a href="example2.com/page/3">Click Me</a>
<a href="example2.com/page/4">Click Me</a>

example2.com

echo $_SERVER['HTTP_REFERER'];

The result is blank. I clicked on the link. How do i solve this? Can $_SERVER['HTTP_REFERER'] work with external domains?

Upvotes: 1

Views: 315

Answers (2)

ColinMD
ColinMD

Reputation: 964

It cant be relied upon.

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.

PHP Manuel

Upvotes: 1

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

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.

Refer to the PHP Manual

To prevent this error try

if(isset($_SERVER['HTTP_REFERER'])) {
  echo $_SERVER['HTTP_REFERER'];
}

OR

echo isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

Upvotes: 2

Related Questions