Welcome to Geeklog Tuesday, November 19 2019 @ 03:25 pm EST

Geeklog Forums

Highlighting Google queries


Status: offline

Dirk

Site Admin
Admin
Registered: 12/01/02
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: [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, '?'Wink;
if ($qpos !== false) {
$qref = substr($ref, $qpos + 1);
$parts = explode('&', $qref);
foreach ($parts as $part) {
if (substr($part, 0, 2) == 'q='Wink {
$q = urldecode(substr($part, 2));
if (substr($q, 6) != 'cache:'Wink {
$query = $q;
}
break;
}
}
}
}
}

return $query;
}[/code]

Next, add this piece of code to your article.php: [code]if (empty($query)) {
$query = CUSTOM_ref2query();
}[/code]
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
 Quote

All times are EST. The time is now 03:25 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