Welcome to Geeklog Monday, October 23 2017 @ 10:08 pm EDT


Status: offline

Dirk

Site Admin
Admin
Registered: 12/01/2002
Posts: 13073
Location:Stuttgart, Germany
You may have seen this on other sites: You're searching for something in Google, click on a result and that site you end up on has the keywords you were searching for highlighted.

Here's how to do that in Geklog:

First, add this function to your lib-custom.php:
PHP Formatted Code
function CUSTOM_ref2query()
{
    global $_CONF;

    $query = '';
    if (!empty($_SERVER['HTTP_REFERER'])) {
        $ref = $_SERVER['HTTP_REFERER'];
        if (substr($ref, 0, strlen($_CONF['site_url'])) != $_CONF['site_url']) {
            $qpos = strpos($ref, '?');
            if ($qpos !== false) {
                $qref = substr($ref, $qpos + 1);
                $parts = explode('&', $qref);
                foreach ($parts as $part) {
                    if (substr($part, 0, 2) == 'q=') {
                        $q = urldecode(substr($part, 2));
                        if (substr($q, 6) != 'cache:') {
                            $query = $q;
                        }
                        break;
                    }
                }
            }
        }
    }

    return $query;
}


Next, add this piece of code to your article.php:
PHP Formatted Code
if (empty($query)) {
    $query = CUSTOM_ref2query();
}

The best place to add this is probably before the first DB_query call (the one that reads SELECT COUNT(*) AS count FROM ...).

To see the effect, try searching Google for something that you know will result in a hit for one of your articles. To see it here on geeklog.net, try "geeklog robots.txt".

The above code makes use of the referrer (which contains the URL of the results page in Google) and the fact that pretty much every search engine uses a parameter q=... that contains the actual search query. So we don't need to check if the request came from Google, Yahoo, or somewhere else.

Have fun.

bye, Dirk

All times are EDT. The time is now 10:08 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content