Placed in: Home arrow Programming arrow Webdesign arrow Check where visitors came from & give them a message
Check where visitors came from & give them a message

Recently, I was stumbling over the net and came across an article showing me the following message:
Hello StumbleUpon user! If you like this article, consider to give it a thumbs up.
It was as if the website knew where I came from. I wanted to know to code behind this.

I did some research and found out how you can find the referring website with a PHP variable. The mfReferral Joomla! module uses this technique too.

Check where your visitors came from & give them a nice, personal message. This way, you'll have a more interactive & personal site. By doing this, you can kindly ask your visitors to do something, like give you a digg when they came from Digg.com. Ask them if they found what they were looking for, if they came from Google.

Here is a "clean" version of the script, that you can use on any PHP based website. Just set your variables in the top of the script & you're ready to go.

Make sure you read the README.txt before configuring the file.

Example messages

Here are a couple of example messages you can display to your visitors.

Welcome Digg user! If you like this article, please consider a digg.

Welcome Google user! Did you find what you were looking for? Have a nice stay.

Welcome Stumbleupon user! If you like this article, simply give it a thumbs up.

Note: This is not a live demo, these are just examples.

Basic setup

The key to achieve this, is by using the following PHP server variable:

$_SERVER['HTTP_REFERER']
This will return the full referral URL where the user came from.

Now, we'll need to trim down this full URL to get only the website. I know two ways to trim down the full URL to only get the website. The first one is by using the explode function, the second with the preg_match function.

// Get the server HTTP Referer
$referral = $_SERVER['HTTP_REFERER'];
// All to lowercase
$referral = strtolower($referral);
// Only get the referral website
$referral = explode ("/", $referral);
$referral = $referral[2];
or
// Get the server HTTP Referer
$referral = $_SERVER['HTTP_REFERER'];
// All to lowercase
$referral = strtolower($referral);
// Only get the referral website
preg_match("/^(http:\/\/)?([^\/]+)/i", $referral, $result);
$referral = $result[2];
There you have it: You now stored the full referral URL in the variable called $referral. Use this variable to show users their own, personal message.

There is a little catch over here. This technique doesn't filter out the subdomain referer. There is a difference now between users from http://username.blogspot.com/ and http://www.blogspot.com/. There even is a difference between users from http://blogspot.com/ and http://www.blogspot.com/! The best way to avoid this, is by removing the subdomain.

// Remove subdomain from the Referer
$referral = explode (".", $referral);
$i = count($referral);
$referral = $referral[$i-2] . "." . $referral[$i-1];
And if you only want to remove the "www." part (for example, if you do want a difference between http://username.blogspot.com/ & http://anotherusername.blogspot.com/), use the following:
// Remove www. from the Referer
if (substr ($referral, 0, 4) == "www.")
{
  $referral = substr ("$referral", 4);
}
And now you're all set to give your users a personal message by locating where they came from. Here's an example:
if ($referral == "marcofolio.net")
{
  echo "Hey! You just came from an awesome website."
}

Advanced configuration

The message above is send to users who came from Marcofolio.net. But what if a visitor came from a website that isn't defined, but you do want to give him a message? Just do the following:

if ($referral == "marcofolio.net")
{
  echo "Hey! You just came from an awesome website."
}
else
{
  echo "Hey, I see you're new! Subscribe to my feed if you like it here."
}
Now every visitor that didn't came from Marcofolio.net will get the second message.

But once again, there is a catch over here too. Visitors from your own website will get the second message too (when navigating through the site)! To fix this, use this PHP server variable

$_SERVER['HTTP_HOST']
And now, for a final solution:
// Exclude your own domain or direct page loading
$urlstring = $_SERVER['HTTP_HOST'];
preg_match("/^(http:\/\/)?([^\/]+)/i", $urlstring, $result);
$domain = $result[2];
// Remove any subdomains from domain
$domain = explode (".", $domain);
$i = count($domain);
$domain = $domain[$i-2] . "." . $domain[$i-1];
// Referral checking
if ($referral != $domain)
{
  if ($referral == "marcofolio.net")
  {
    echo "Hey! You just came from an awesome website."
  }
  else
  {
    echo "Hey, I see you're new!"
  }
}

User friendly

Instead of such an simple (and pretty ugly) echo, you could display an image or create a CSS valid alert message.

Use my script

As I mentioned above, I used this technique in the mfReferral Joomla! module.

  1. Standard support for over 25 websites, including Digg, StumbleUpon, Reddit, del.icio.us and much more.
  2. Add up to 5 custom websites
  3. Creates a CSS valid message & adds an icon
  4. Message doesn't appear to users browsing your website or visiting your site through a direct link
  5. Message box is very flexible and highly customizable
  6. Ability to show buttons (Such as the Digg button)
  7. Set your own custom text for each website
And you can download it here for free. Implement it on your website by using the include function. If there are any Wordpress / Drupal / any other CMS developers out there, feel free to bring it to your CMS of choice.

Just make sure that you read the README.txt file before trying anything.

If you want to test the script, simply install RefControl. This Firefox extension allows you to set your own refferal ID.

Attribution-Share Alike 3.0

Feel free to give feedback about this PHP script. Enjoy using it!


Tags:  free php webdesign internet blog

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
K. Fields - Great comment box!   2008-07-07 20:27:10
Gravatar image What I would like to know is where did you get this great comments section?
dennis - can't get it to work!   2009-03-12 21:00:01
Gravatar image :?:
I have done what I needed to do, but the php script does not seem to work. What have i done wrong?

please take a look at www.seo-boek.com it is all in there. Also the php script is in place at http://www.seo-boek.com/referral.php


PLEASE HELP, i would love to have this script.

- this comment box is great. would love to know where to get it!

TNX!
Dinko   2009-05-05 15:11:18
Gravatar image :?:

Not working for me, either. :(
Dinko   2009-05-05 21:52:01
Gravatar image It's O.K., i get it. Sorry, my mistake. Thanks! :)
Charlie Sheather   2009-05-09 06:36:37
Gravatar image Ok, been trying to get this to work for an hour or so but it just will not.

http://127.0.0.1/fullershop/cookie/2/ thats where the code is, and ive just put in an option for marcofolio.net it should say:

"youve come from marcofolio.net"

Charlie
Charlie Sheather   2009-05-09 06:38:50
Gravatar image XD just relised u cant go there, uploading to http://bigsandpit.com/charlie/cookie/2/

Charlie
Dave Carter - Great script...but how???   2009-11-01 08:11:02
Gravatar image Hi,

Many thanks for a great script, the problem I have is I am looking for a referral script that shall display the relevant message through out all my own site pages. So if a visitor came from google to my site / index it would say "welcome googler" and if they then clicked on my site / internal links and visited a new page on my site the java script referrer still knows they originally came from google and displays again " welcome googler"

Is this possible with this script?

Thanks[
Anonymous - re: Great comment box!   2009-11-07 13:06:14
Gravatar image
K. Fields wrote:
What I would like to know is where did you get this great comments section?
Dave Carter wrote:
Hi,

Many thanks for a great script, the problem I have is I am looking for a referral script that shall display the relevant message through out all my own site pages. So if a visitor came from google to my site / index it would say "welcome googler" and if they then clicked on my site / internal links and visited a new page on my site the java script referrer still knows they originally came from google and displays again " welcome googler"

Is this possible with this script?

Thanks[
best pharmacy website reviews - best pharmacy website reviews   2011-06-10 10:28:33
Gravatar image I am strongly associated with this site. As this site has inspired me a lot always in a new way and made my work easy by every time highlighting on the new issue and make me pleased. Thanks you people rock!!!!!!
prescription birth control pil - prescription birth control pills dosage   2011-06-10 10:29:05
Gravatar image I am frequent reader of the articles and new knowledgeable post about new things always and would be searching new stuff for that. And I really thank you people for providing us new articles and post. Thanks a lot!
prescription birth control pi - prescription birth control pills   2011-06-10 10:29:37
Gravatar image It was really surprising to see such a wonderful post that is inspiring and informative and caught the attention of many people. I am a regular visitor of the blog and love the work of these people.
over weight dosage guidelines - over weight dosage guidelines   2011-06-10 10:31:01
Gravatar image This is a wonderful website that has great info and is helpful for one and all. I always look forward for your website to gather any kind of information. Hope you people do like this only wonderful job. Cheers
hot drug store reviews - hot drug store reviews   2011-06-10 10:32:02
Gravatar image I often like surfing on net and find info on new things and this time I got a new website which has great info and is quite brilliantly written. Am just thrilled and excited to see this and hope to see more work of you people in future.
abdul basit - astrology   2012-04-26 12:34:05
Gravatar image aoa
abdul basit   2012-04-26 12:34:32
Gravatar image aoa
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio