Monday, March 5, 2012

PHP function that get query from search engine

SEO (Search Engine Optimisation) take an important role for the success of your online business. For someone you may not know what is SEO, here is the definition from wiki.

Search engine optimization (SEO) is the process of improving the visibility of a website or a web page in search engines via the "natural," or un-paid ("organic" or "algorithmic"), search results. In general, the earlier (or higher ranked on the search results page), and more frequently a site appears in the search results list, the more visitors it will receive from the search engine's users. SEO may target different kinds of search, including image search, local search, video search, academic search,news search and industry-specific vertical search engines.
Indeed, you may want to know more how your customers go to your site through search engine. This is a function to track the search query on search engine before a user goes to your site.

function search_engine_query_string($url = false) {

    if(!$url) {
        $url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false;
    if($url == false) {
        return '';

    $parts = parse_url($url);
    parse_str($parts['query'], $query);

    $search_engines = array(
        'about' => 'terms',
        'alice' => 'qs',
        'alltheweb' => 'q',
        'altavista' => 'q',
        'aol' => 'q',
        'aol.' => 'query',
        'aol..' => 'encquery',
        'answers' => 's',
        'aolsearch' => 'q',
        'ask' => 'q',
        'baidu' => 'wd',
        'bing' => 'q',
        'cnn' => 'query',
        'daum' =>'q',
        'eniro' => 'search_word',
        'ekolay' => 'q',
        'google' => 'q',
        '' => 'as_q', // seen on
        '' => 'q',
        'kvasir' => 'q',
        'live' => 'q',
        'lycos' => 'query',
        'mamma' => 'query',
        'msn' => 'q',
        'mynet' => 'q',
        'naver' => 'query',
        'najdi' => 'q',
        'netscape' => 'query',
        'onet' => 'qt',
        'ozu' => 'q',
        'pchome' => 'q',
        'rambler' => 'words',
        'search' => 'q',
        'sesam' => 'q',
        'seznam' => 'q',
        'sNOWsh' => 'q',
        'szukacz' => 'q',
        'terra' => 'query',
        'voila' => 'rdata',
        'virgilio' => 'qs',
        'wp' => 'szukaj',
        'yahoo' => 'p',
        'yam' => 'k',
        'yandex' => 'text'

    preg_match('/(' . implode('|', array_keys($search_engines)) . ')\./', $parts['host'], $matches);

    return isset($matches[1]) && isset($query[$search_engines[$matches[1]]]) ? $query[$search_engines[$matches[1]]] : '';


Books you may feel interested:
Related Posts Plugin for WordPress, Blogger...