You are not logged in.
- Topics: Active | Unanswered
Pages: 1
#1 2008-11-11 00:20:39
- orkneywd
- Member

- Registered: 2008-05-10
- Posts: 96
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 ![]()
Offline
#2 2008-11-11 01:46:25
- sirena
- Member

- From: AU
- Registered: 2008-05-10
- Posts: 172
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
Offline
#3 2008-11-11 03:31:03
- orkneywd
- Member

- Registered: 2008-05-10
- Posts: 96
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.htmlOr 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
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 ![]()
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
Thanks again
Offline
#4 2008-11-11 04:17:58
- Tyler
- Member
- Registered: 2008-05-11
- Posts: 104
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.
Offline
#5 2009-10-19 20:40:34
- rdcAmy
- Member
- Registered: 2009-10-19
- Posts: 1
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.
Tyler, I know this thread is a bit old, but would you mind giving me an example of how I could pull the info from [INSERT HTML STUFF HERE] into a specific spot on the page? I am trying to swtich out phone numbers based on referring site. Or can i simply paste this code into that spot in the body of my code where I want the phone to switch?
Also, does my page have to be written in PHP to use this? I think the client's site is strictly html. . .
Offline
#6 2009-10-19 20:45:39
- Franz
- Lead developer

- From: Germany
- Registered: 2008-05-13
- Posts: 3,755
- Website
Re: Content based on referer?
You can use that piece of code everywhere in your code, yes.
And if you cannot use PHP, you might be able to use JavaScript for determining the referer.
Offline
#7 2009-11-25 00:35:08
- vlus
- Member
- Registered: 2009-11-25
- Posts: 1
Re: Content based on referer?
Hello,
I am thankful to find this posting, as I have been searching for days. The code posted above appears to be exactly the opposite of what I need, but I am hopeful someone can make it the reverse.
I have an iframed page, and often folks stumble on the iframed content on search engines but it is read out of context with the rest of my website. so, I am hoping for some PHP code I could insert into the iframed content that basically says.... if the referrer is NOT mywebsite.com, then click here to view this page in its proper form.
Any help is greatly appreciated, in advance!
Vlus
Offline
#8 2010-09-25 05:06:58
- curtis
- Member
- Registered: 2010-09-25
- Posts: 1
Re: Content based on referer?
Is there anyway to make this work with subdomains?
I want people to see content from example.com and www.example.com but NOT foosubdomains.example.com
Offline
#9 2010-11-09 17:39:42
- MattF
- Member

- From: South Yorkshire, England
- Registered: 2008-05-06
- Posts: 1,230
- Website
Re: Content based on referer?
Belated response, but any code which relies on the http_referer being set is going to be partially effective at best.
Screw the chavs and God save the Queen!
Offline
#10 2010-11-10 03:14:51
- Jérémie
- Member

- From: Paris, France
- Registered: 2008-04-30
- Posts: 627
- Website
Re: Content based on referer?
You can't trust http_referer in any way.
A lot of common "security package software" (emphasis on the quote, with a large dose of sarcasm) remove these from any web browsing, for instance.
Offline
Pages: 1
