4

I want to customize my index.php when a visitor searchs for a word in google search and comes to my site. ex:

if(visitor searches for "A" in google search)
   do somthing;
elseif(visitor searches for "B" in google search)
   do somthing else;

if i should use referrer tell me how?

M Rostami
  • 4,035
  • 1
  • 35
  • 39

1 Answers1

3

Try this script:

function getKeywords()
{
    $refer = parse_url($_SERVER['HTTP_REFERER']);
    $host = $refer['host'];
    $refer = $refer['query'];

    if(strstr($host,'google'))
    {
        $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);
        $querystring = $output[0];
        $querystring = str_replace('&q=','',$querystring);
        $keywords = explode('+',$querystring);
        return $keywords;
    }
    else
    {
        return false;
    }
}

Note: this does not work when the someone used encrypted Google (which uses SSL): https://encrypted.google.com/

Jeroen
  • 13,056
  • 4
  • 42
  • 63
  • thank you that's grate function. but if i want to get this when someone used google.com with https what should i do? – M Rostami May 12 '12 at 12:26
  • 1
    There's no way, sorry. Read more about this here: http://stackoverflow.com/questions/7620328/is-there-referrer-header-while-using-ssl – Jeroen May 12 '12 at 12:29
  • 1
    Quote from Wikipedia: `If a website is accessed from a HTTP Secure (HTTPS) connection and a link points to anywhere except another secure location, then the referrer field is not sent.` – Jeroen May 12 '12 at 12:29