I purchased a search script recently and for the cache it just had a hyperlink to googles cache, I didnt want this, I wanted it on my own site, as if it was my own cache, I couldnt find anything already made so I created this small php script.
To use the cache simply encode the URL you want the cache of
eg if you have $searchkw and $url in your current search script, and you want to create a hyperlink to the cache, you would simply do:
Code:
<?php
$enc_url = base64_encode($url);
echo"<a href=\"cache.php?u=$url&q=$searchkw\">Cached Version</a>\n";
?>
If you dont want keyword highlighting on, simply remove the &q=$searchkw
My example:
http://plerocs.com/result/20/chuck+norris
Click on cached page to see the script in action.
Code:
<?php
$url = $_GET['u'];
$url = base64_decode($url);
$kw = $_GET['q'];
$kw = str_replace(" ", "+", "$kw");
$useragent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://209.85.173.132/search?q=cache:$url+$kw");
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER,"http://google.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
$contents = str_replace("This is Google's cache of", "This is our cache of", "$contents");
$contents = str_replace(" <a href=\"http://www.google.com/intl/en/help/features_list.html#cached\" style=\"text-decoration:underline;color:#00c\">Learn more</a>", "", "$contents");
$contents = str_replace("<a href=\"http://209.85.173.132/search?q=cache:$url+$kw&hl=en&strip=1\" style=\"text-decoration:underline;color:#00c\">Text-only version</a>", "", "$contents");
$contents = str_replace("These search terms are highlighted", "Your search terms are highlighted", "$contents");
if(strpos($contents, "</b> - did not match any documents. <br>"))
{
echo"<h1>Error</h1>\n";
echo"<hr>\n";
echo"Unfortunately the page you wanted does not yet exist in our cache. You may visit the page by clicking the link below:<br>\n";
echo"<a href=\"$url\" rel=\"nofollow\">$url</a>\n";
}else{
echo"$contents";
}
?>