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.
Text 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;
}
Text 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.