Placed in: Home arrow Programming arrow Webdesign arrow Check where visitors came from & give them a message comprar viagra en espaƱa
comprar viagra online
comprar viagra generico barato
comprar cialis generico online
comprar cialis online seguro
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!
 
< Prev   Next >