0

I'm trying to receive the referrer URL. If an external website has a link to my website and someone clicks on that link, I want to receive (and save) that external website's URL. After clicks on my website, I still need that same referrer URL.

I've tried the following code:

<?php
if (isset($_SERVER['HTTP_REFERER'])) {
  $refURL = $_SERVER['HTTP_REFERER'];
  echo $refURL;
} else {
  echo "No referer URL";
}
?>

But if you click on the link on the external website, it doesn't show anything. If I click on a link on my website, I always get the previous page.

It seems that I don't get the URL if I'm redirected from an external website.

Dennis
  • 528
  • 3
  • 24
  • Take a look at this question. There is no answer to it yet but it will help you understand what to expect for a referer. Also do note that the default policy for recent browsers is `strict-origin-when-cross-origin` and for older browsers is `no-referrer-when-downgrade`. https://stackoverflow.com/questions/68175415/understanding-different-referrer-policy-and-what-is-sent-when – endeavour Jun 30 '21 at 07:25
  • Also note a redirect is not same as a click. A redirect can be 30X and as per my tests at least a 301 and 302 redirect will simply copy the referer of the source (ie. redirecting page) – endeavour Jun 30 '21 at 07:27
  • Whether you get the `referer` or not from an external website will depend on the `Referrer-policy` set by the external website. So if the policy is `strict-origin-when-cross-origin` (default for recent browsers) and if the source page is `https or http` and your page is `https` then you should expect only the `Origin` in the referer. If yours is an `http` page and source is `https` you will not see a referer. The question I linked earlier explains it and also the concept of `origin` as well as whats included in a `full url`. – endeavour Jun 30 '21 at 07:34

0 Answers0