How to Get the Referral URL in PHP?

It is often necessary to detect and get the referral URL in PHP. For example, in affiliate marketing referral URL plays a great role. By detecting referral URL it is decided which affiliate partners send the traffic or customers to the site.

Also, referral URL uses to get the traffic sources of a website.

Now in this post, I am going to show you how to detect and get the referral URL in PHP and display it on the web page.

Getting the referral URL is quite easy in PHP. There is a global variable available in PHP which contains the referral URL.┬áThe $_SERVER global variable has the element “HTTP_REFERER” that will return the referer URL.



$_SERVER[‘HTTP_REFERER’] is the element or code that returns the complete URL of the web page from where the current page was called.

Below is the PHP code snippet that will first check and if referral URL exists or not and then display that URL on our web page:

<?php
// Check if Referral URL exists
if (isset($_SERVER['HTTP_REFERER'])) {
  // Store Referral URL in a variable
  $refURL = $_SERVER['HTTP_REFERER'];
  // Display the Referral URL on web page
  echo $refURL;
} else {
  echo "No referer URL";
}
?>

Now if the link of our web page available on any other web page and one come to our page by clicking the link, then we will able to see the page URL where we click the link to our website. Thus we will be able to see the referral URL that sends traffic to our web page or website.

 

Also, read:

 

But if one visits the page directly, then it will display the message “No referer URL”.

We have used PHP isset() to check if referral URL exists. Otherwise, it would show undefined index error.

I hope you have learned how to get the referral URL using PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *