Topic: Content based on referer?

I've been playing with referrers on localhost somewhat unsuccessfully but have the following working on two test domains. I'm trying to show different content based on the referer, so anyone finding my site through any other means than fluxbb/punbb.org will see my normal portfolio site. Those coming from flux/pun will see my skins/mods etc.

<?php

$match = false;
$sites = array("fluxbb.org", "mynormalsite.com");

if(strlen($_SERVER['HTTP_REFERER']))
{
    $referer = parse_url($_SERVER['HTTP_REFERER']);
           
    $referer['host'] = str_replace("www.", "", strtolower($referer['host']));

    $match = in_array($referer['host'], $sites);
}

if($match)
{

?>

<html>
<head>
<title>My FluxBB Portfolio</title>
</head>
<body>
<p>Welcome FluxBB user! Here you can find my skins and mods free to download.</p>
</body>
</html>

<?php
}
else
{
?>

<html>
<head>
<title>My Non-FluxBB/PunBB Related Stuff</title>
</head>
<body>
<p>Welcome to my portfolio of non-fluxbb related things.</p>
</body>
</html>

<?php
}
?>

I'm looking to throw a third site into the array so that users coming from PunBB will see my "PunBB mods" page, whereas users coming from FluxBB will see my "FluxBB mods" page. If the referrer is blank or doesnt match fluxbb.org/punbb.org it should just show my normal site.

If someone can code up a basic working example with a third site loaded in the array (and a seperate third html sheet) I'd be glad to shoot over some beer money through paypal. Cheers smile

Re: Content based on referer?

There may also be an easier way to achieve this at the Apache level, through mod_rewrite and .htaccess, along the lines of

RewriteEngine on
# Options +FollowSymlinks
RewriteBase /www/website/html/
RewriteCond %{HTTP_REFERER} ^http://fluxsite\.com/somedirectory [NC]
RewriteRule pageforflux.html 

Or something along those lines. More info:
http://httpd.apache.org/docs/2.2/rewrite/

Also in .htaccess (or httpd.conf), you may even be able to do it in an even simpler way with the Apache "SetEnvIf Referer" environment variable, although I haven't checked that option out completely:
http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html

Re: Content based on referer?

sirena wrote:

There may also be an easier way to achieve this at the Apache level, through mod_rewrite and .htaccess, along the lines of

RewriteEngine on
# Options +FollowSymlinks
RewriteBase /www/website/html/
RewriteCond %{HTTP_REFERER} ^http://fluxsite\.com/somedirectory [NC]
RewriteRule pageforflux.html 

Or something along those lines. More info:
http://httpd.apache.org/docs/2.2/rewrite/

Also in .htaccess (or httpd.conf), you may even be able to do it in an even simpler way with the Apache "SetEnvIf Referer" environment variable, although I haven't checked that option out completely:
http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html

Thanks for the links Sirena smile Just taken a look (albeit a quick one as its late over here) and it looks like the .htaccess method could suit my needs. I'll take a better look over the pages tomorrow though the downside (in my opinion) to the .htaccess method is that it redirects users to a completely seperate URL. I liked the thought of having multiple homepages, all index.php, and having them serve individual content based on where the users came from.

As I'm still learning PHP it was more curiousity as to why I couldn't get 3 URLs into the PHP array. Having looked online and thoroughly read my "PHP In Easy Steps" book I just couldn't get it working. Either I'm not understanding how arrays work, or I'm not understanding how referers work sad

I'd still be interested in seeing a PHP version if anyone can assist?

I think a similiar PHP script could be put to good use on ecommerce stores. If the referer URL is "http://www.google.co.uk/search?hl=en&q=cheap+dvds" you could use an array of pre-selected words to display appropriate announcements enticing those users to make a purchase. In this example the user obviously has a limited budget so an announcement could direct them to your clearance lines, or better yet display a 10% off coupon code. If the referrer doesn't contain the word "cheap" they can pay full price big_smile

Thanks again

Re: Content based on referer?

<?php

if(strlen($_SERVER['HTTP_REFERER']))
{
    $referer = parse_url($_SERVER['HTTP_REFERER']);
    $referer['host'] = str_replace("www.", "", strtolower($referer['host']));
}

switch ($referer['host'])
{
    /* If the referer is fluxbb.org */
    case 'fluxbb.org':
        [INSERT HTML STUFF HERE]
        break;

    /* If the referer is punbb.org */
    case 'punbb.org':
        [INSERT HTML STUFF HERE]
        break;

    /* If the referer is google.com */
    case 'google.com':
        [INSERT HTML STUFF HERE]
        break;

    /* If the referer is anything else */
    default:
        [INSERT HTML STUFF HERE]
        break;
}

The switch decides which set of HTML to use by finding out if the value of $referer['host'] has a case.  If it doesn't, it automatically uses the default case.