PHP Script - Get search engine query string

Discussion in 'PHP' started by adzeds, Feb 3, 2010.

  1. #1
    I am using a script to try and capture the keywords used to access my website.

    I can't seem to find the problem. I am taking the returned value and storing it in a mySQL database, but when I test it I just fet 'Array' stored in the DB.

    Here is the script I am using, where am I going wrong?

    function check_se_keywords()
    {
        $refer = parse_url($_SERVER['HTTP_REFERER']);
        $host = $refer['host'];
        $refer = $refer['query'];
        
        if(strstr($host,'google'))
        {
            //do google stuff
            $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('&q=','',$querystring);
            $keywords = explode('+',$querystring);
            return $keywords;
        }
        elseif(strstr($host,'yahoo'))
        {
            //do yahoo stuff
            $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('p=','',$querystring);
            $keywords = explode('+',$querystring);
            return $keywords;
            
        }
        elseif(strstr($host,'msn'))
        {
            //do msn stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            return $keywords;
        }
        else
        {
            //else, who cares
            return false;
        }
    }
    PHP:
     
    adzeds, Feb 3, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    straight from the explode manual page :

    Returns an array of strings

    http://us2.php.net/explode

    try something like
    $keyword_list = implode(",",$keywords);
    return $keyword_list;

    should give keyword1,keyword2 etc.
     
    shallowink, Feb 3, 2010 IP
  3. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #3
    That does not seem to make any difference!
     
    adzeds, Feb 3, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    $keywords is an array, if you do return $keywords then all it will give is "Array" because no array value is defined.

    do this:

    function check_se_keywords($input)
    {
        $refer = parse_url($input);
        $host = $refer['host'];
        $refer = $refer['query'];
       
        if(strstr($host,'google'))
        {
            //do google stuff
            $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('&q=','',$querystring);
            $keywords = explode('+',$querystring);
            return $keywords[0];
        }
        elseif(strstr($host,'yahoo'))
        {
            //do yahoo stuff
            $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('p=','',$querystring);
            $keywords = explode('+',$querystring);
            return $keywords[0];
           
        }
        elseif(strstr($host,'msn'))
        {
            //do msn stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            return $keywords[0];
        }
        else
        {
            //else, who cares
            return false;
        }
    }
    PHP:
     
    danx10, Feb 3, 2010 IP
  5. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #5
    Cheers.

    I have it working now like this:

    
    function check_se_keywords()
    {
        $refer = parse_url($_SERVER['HTTP_REFERER']);
        $host = $refer['host'];
        $refer = $refer['query'];
        
        if(strstr($host,'google'))
        {
            //do google stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            $keywords = implode(' ', $keywords);
            return $keywords;
           
        }
        elseif(strstr($host,'yahoo'))
        {
            //do yahoo stuff
            $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('p=','',$querystring);
            $keywords = explode('+',$querystring);
            $keywords = implode(' ', $keywords);
            return $keywords;
            
        }
        elseif(strstr($host,'msn'))
        {
            //do msn stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            $keywords = implode(' ', $keywords);
            return $keywords;
        }
        elseif(strstr($host,'bing'))
        {
            //do bing stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            $keywords = implode(' ', $keywords);
            return $keywords;
        }
        elseif(strstr($host,'ask'))
        {
            //do ask jeeves stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            $keywords = implode(' ', $keywords);
            return $keywords;
        }
        else
        {
            //else, who cares
            return false;
        }
    }
    
    PHP:
     
    adzeds, Feb 3, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Glad, you got it working although, you don't really need an extra line of code when you could just do
    return $keywords[0];
    Code (markup):
     
    danx10, Feb 3, 2010 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    And instead of this:
    $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
    $querystring = $output[0];
    $querystring = str_replace('p=','',$querystring);
    PHP:
    You can just do this (change 0 to 1):
    $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
    $querystring = $output[1];
    PHP:
     
    SmallPotatoes, Feb 3, 2010 IP
  8. noname0

    noname0 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Im using this one, give it a try

    
    
    $referrer = $_SERVER['HTTP_REFERER']; 
    if(preg_match("/[\.\/](google|yahoo|bing|geegain|mywebsearch|ask|alltheweb)\.[a-z\.]{2,5}[\/]/i",$referrer,$search_engine)){ 
    $referrer_query = parse_url($referrer); 
    $referrer_query = $referrer_query["query"]; 
    $q = "[q|p]"; //Yahoo uses both query strings, I am using switch() for each search engine 
    preg_match("/".$q."=(.*?)&/",$referrer,$keyword); 
    $keyword = urldecode($keyword[1]); 
    echo $keyword;
    }
    
    
    PHP:
     
    noname0, Feb 3, 2010 IP
  9. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #9
    @noname0
    That looks like a good script!
     
    adzeds, Feb 4, 2010 IP
  10. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #10
    I have found a problem that I could use some help with.

    My script is working well at picking out keywords, or so I thought. After looking through my data in my database I have discovered that users who search for my domain name don't get caught properly.

    Example:
    Search google for "www.eaguingamp.co.uk" it sends the following to my http_referer:

    http://www.google.co.uk/url?sa=t&so...R4nnNtuJtyznAilZQ&sig2=175q1KTF36lsLf-7zRRr9g

    When this comes to my database all that gets stored is 'www' it is ignoring everything after the '.'.

    Here is what I am processing the string with
    
            //do google stuff
            $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
            $querystring = $output[0];
            $querystring = str_replace('q=','',$querystring);
            $keywords = explode('+',$querystring);
            $keywords = implode(' ', $keywords);
            return $keywords;
    
    PHP:
    How can I fix this problem?
     
    adzeds, Feb 15, 2010 IP
  11. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #11
    noone can help with this problem?
     
    adzeds, Feb 16, 2010 IP
  12. WeX09

    WeX09 Member

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #12
    @noname0 your script is really great! - How can the keywords be grouped and sorted?

    Thanks in advance
     
    WeX09, Apr 11, 2011 IP