How to get the Referral URL using PHP & ASP.NET?

Assume that you are developing a website which is going to be live on the Internet and you wish to know how people reach your website. Most of them would be coming through the search engines – Google, Yahoo or Bing or some will be coming through the blogs such as these.  These URLs which drives the traffic to your website are known as the Referrals and they can be quite useful to know how your traffic reaches you.   In order to get these URLs, you need several server commands on your webpage and we are talking about PHP and ASP.NET webpages here.

In PHP :

In PHP, we use the variable $_SERVER[’HTTP_REFERER’] to get the referral URL.  All you need to do it is, create a session and save the referral URL onto a variable and store it on your database if you wish.

Sample Program :

< ?php
     session_start();     // Starting the Session
if(!isset($_SESSION[’referralurl’])){  //Getting the Session

       $referralurl = $_SERVER[’HTTP_REFERER’];  //Getting the Referral URL and storing it
       $referralurl = “Unknown Referral”;  //If not found

   $_SESSION[’referralurl’] = $referralurl; //Saving the session



In ASP.NET, the procedure is the same.  You just have to use the HttpRequest.UrlReferrer Property.

Program :

Uri RefURL = Request.UrlReferrer;
 Response.Write("Referrer URL Port: " + Server.HtmlEncode(RefURL.Port.ToString()) + "<br/>");
 Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(RefURL.Scheme) + "<br/>");

ElamParithi Arul is Editor At Large and the Owner of Tech Twinklers. He is the founder of the Parithi Network. Basically being a 23 year old Whizkid, He's A Mac User. An Adventurer. A Tech Enthusiast, An Avid PC and PlayStation Gamer, A Hobbyist Photographer and a Nature Lover. His basic interests are developing Websites, Apps and coming up with various creative ideas for a better vibrant tomorrow. He is a professional blogger for about 4 years and writes blogs regarding technology, gaming, and about his own personal life. You can follow him on Twitter at @Parithi

You may also like


Welcome To TechTwinklers!

Tech Twinklers is a Tech Blog run by Students with an aspiring enthusiasm in Technology and Gaming. This Blog will bring News about the Modern Technology, Educational Advances, Campus Interviews, etc. So make sure you subscribe to our blog because we don't want you to miss a thing that is posted here..

We're on Facebook!