Forum Moderators: coopster

Message Too Old, No Replies

Using referrer information in a PHP script

User googles 'widget'. Clicks my site. Messages say 'looking for widgets?'

         

roddy

3:11 pm on Aug 21, 2003 (gmt 0)

10+ Year Member



I think I've pretty much summed up what I want to do in the Meta Description, but I'll try and explain in more detail.

People find my site through Google (y'know, the search engine). When they click through to my site, the search words and phrases are passed to the server - they turn up in my stats package at least . . .

What I want to know is - can I make that information available to a PHP script running on my page? For example, user searches for 'widgets'. My site comes up at no.1, naturally. User clicks on the link to my page, and a PHP script echos 'So, you're looking for info about widgets' onto the top of the page.

Obviously, there would be a default option for non-search engine traffic.

I hope that's clear. Is it possible? I've posted this in the PHP forum as I'm less incompetent with PHP than I am with other scripting languages - if something else would be more suitable, I'd still be interested.

Thanks

Roddy

mogwai

5:07 pm on Aug 21, 2003 (gmt 0)

10+ Year Member



You can use $_SERVER["HTTP_REFERER"] to get the referrer, you'll need some regex to extract the search query.

coopster

5:17 pm on Aug 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, using HTTP_REFERER is not a good practice. First off, it can be spoofed. Secondly it can be turned off by the user agent. And lastly, never trust browser-supplied variables. A quick search on the net for "HTTP REFERER" will give you loads of information on this practice.

However, for those user agents that do have it turned on, you can customize the welcome message. Everyone else would get your default option.

mogwai

10:12 pm on Aug 21, 2003 (gmt 0)

10+ Year Member



"Actually, using HTTP_REFERER is not a good practice."

Which other referrer variable would you suggest can be used to answer this particular question?

panic

10:50 pm on Aug 21, 2003 (gmt 0)

10+ Year Member



It's going to take more work that you guys make it sound, but here's essentially what you need to do :
Try this on for size :


if(!isset($HTTP_REFERER)){
print "General message goes here.";
}
else{
$query = split("[?]",$HTTP_REFERER);
$div = split("[&]",$query[1]);
foreach($div as $var){
if(preg_match("/q=/",$var)){
$var = ereg_replace("q=","",$var);
$var = ereg_replace("+"," ",$var);
print "So, you're looking for info about $var";
}
else{
}
}
}

GeorgeGG

1:28 am on Aug 22, 2003 (gmt 0)

10+ Year Member



Maybe you could also change:

if(!isset($HTTP_REFERER)){
print "General message goes here.";
}

to something like:


if(!isset($HTTP_REFERER))
{
print "<script TYPE=\"text/javascript\">\n";
print "if(document.referrer)\n{\n";
print " var args = new Object();\n";
print " var query = document.referrer.substring(1);\n";
print " var pairs = query.split(\",\");\n";
print " document.write(\"So, you\'re looking for info about \");\n";
print " for(var i = 0; i < pairs.length; i++)\n {\n";
print " var pos = pairs[i].indexOf(\"=\");\n";
print " if(pos == -1){continue;}\n";
print " var value = pairs[i].substring(pos+1);\n";
print " document.write(unescape(value));\n }\n}\n</script>\n";
}

That way if you don't get it on the server you have a chance to get it from JS.

GGG

dmorison

3:39 am on Aug 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't parse the referrer string and extract the exact query - you could end with a page looking like..

Hey, looking for info on knick knack paddywhack give your dog a bone

..which doesn't look good.

Instead, simply do a strstr() on $_SERVER["HTTP_REFERER"] for your primary keywords, and then display relevant welcome text for those keywords.

That way you're in complete control of the text, and are still creating a relevant page, for example:

$referer = strtolower($_SERVER["HTTP_REFERER"]);

if (strstr($referer,"dog"))
{
echo("<h1>Dogs simply love our dog food!</h1>");
}
else if (strstr($referer,"bee"))
{
echo("<h1>Bees make a beeline for our bee food!</h1>");
}
else if (strstr($referer,"cat"))
{
echo("<h1>Cats can't get enough of our cat food!</h1>");
}
else if (strstr($referer,"hamster"))
{
echo("<h1>Our hamster food makes your hammy happy!</h1>");
}
else
{
echo("<h1>If your pet could order pet food online, this is where they'd come!</h1>");
}