Get a Referral URL from the Session using PHP

Looking to associate a lead or visitor to a source?

Senario: Your marketing department, boss, or client want more information about the people contacting them through their website and where they are coming from. They want to tie an ROI value to the money they are spending on Google, Yahoo, MSN, ad networks, social media, etc.

Don’t worry there are several ways to do this based on the type of language the web site is built in and the information you are after. I will give several examples and other scenarios down the road, but for now let’s start by pulling the session on a php website…This seems to be what a lot of people are looking for.

How to get the URL Referral using PHP from the session.

Using a session you can make sure you only get the URL when people first land on your website. You can do other things as well like set the time period you want the value to stay in the session.

Here’s the sample code you will need to add to the page you are sending visitors to:

< ?php
session_start();
if(!isset($_SESSION[’referrer’])){
//get the referrer
if ($_SERVER[’HTTP_REFERER’]){
$referrer = $_SERVER[’HTTP_REFERER’];
}else{
$referrer = “unknown”;
}
//save it in a session
$_SESSION[’referrer’] = $referrer; // store session data
}
?>

Code Explanation:

The first part is pretty straight forward and checks to see if the referrer URL has already been set:

if(!isset($_SESSION[’referrer’])){ …

If not we set the session variable. If we can’t get the URL it gets set to “unknown” in the above example.

For some reason you can’t always get the referrer URL. Some people have found that links originating from some email applications like Gmail don’t give you a referrer URL, but yahoo does. I assume it has to do with email security and caching old email messages.

Anyways, once you’ve got the session variable set, you can call it any time until the browser window is closed or the variable time limit runs out (the default time limit set in the php.ini, but you can also set it in your script.) You’ve normally got 180 minutes as default for a session time limit before your variable is lost.

Next is to call the variable and store it with the record or visitor information when they fill out a form.

To Call the Session Variable:

To call your session variable from another page, you just need to add this piece of code:

session_start();
echo “referrer = “. $_SESSION[’referrer’]; //retrieve data
?>

Stick the retrieve session data code snip it on your contact form if you want to associate the referring URL to the lead. Your get something like this in your output: “referrer = http://your.referrer.url”. You can store and pull other variables in the session such as landing page information or number of pages visited by incrementing a variable.

These are good resources if you need additional information:

http://uk2.php.net/session (Explains all session runtime configuration options)
http://www.tizag.com/phpT/phpsessions.php (Shows how to capture pageviews)

del.icio.us:Get a Referral URL from the Session using PHP  digg:Get a Referral URL from the Session using PHP  spurl:Get a Referral URL from the Session using PHP  wists:Get a Referral URL from the Session using PHP  simpy:Get a Referral URL from the Session using PHP  newsvine:Get a Referral URL from the Session using PHP  blinklist:Get a Referral URL from the Session using PHP  furl:Get a Referral URL from the Session using PHP  reddit:Get a Referral URL from the Session using PHP  fark:Get a Referral URL from the Session using PHP  blogmarks:Get a Referral URL from the Session using PHP  Y!:Get a Referral URL from the Session using PHP  smarking:Get a Referral URL from the Session using PHP  magnolia:Get a Referral URL from the Session using PHP  segnalo:Get a Referral URL from the Session using PHP

Related Entries

5 Responses to “Get a Referral URL from the Session using PHP”


  1. 1 Oren Yomtov Aug 26th, 2009 at 12:28 am

    Thanks! It helped me.

  2. 2 Web master Sep 19th, 2009 at 12:27 pm

    Very useful example for website visit statistic. Also it is very good for those who want to learn php programming and how to use session.

  3. 3 Kellar Feb 26th, 2010 at 9:47 am

    thanks

  4. 4 Kellar Feb 26th, 2010 at 10:02 am
  5. 5 STEELEMamie20 Apr 5th, 2010 at 4:43 am

    Different people in the world take the loan in various banks, because that is comfortable.

Leave a Reply