
<?
/*
http://www.seomoz.org
Author: Matthew Inman <matt@seomoz.org>
File: whererank.php
Description:
This script searches google for a given search term and redirects the user to the
page of search results that contains the target URL.
Installation:
Rename this file have a .php extension and upload it to your web server.
*/
if (isset($_GET['url']) && isset($_GET['keyword'])) {
$gg_url = 'http://www.google.com/search?hl=en&q=' . urlencode($_GET['keyword']) . '&start=';
$url = preg_replace('(^http://|/$)','',$_GET['url']);
for ($page = 0; $page < 9; $page++) {
$handle = fopen($gg_url . $page . 0 ,'r');
$scraped = '';
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$scraped .= $buffer;
}
fclose($handle);
}
$results = array();
preg_match_all('/a href="([^"]+)" class=l.+?>.+?<\/a>/',$scraped,$results);
foreach ($results[1] as $serp) {
$serp = preg_replace('(^http://|/$)','',$serp);
if ($serp == $url) {
header('Location: ' . $gg_url. $page . '0');
exit;
}
}
}
$error_message = 'Not in top 100 search results';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Where's it rank at Google?</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
form { width: 500px; margin: 0 auto; }
.error_message { color: #920000; }
label {
display: block;
margin-top: 1em;
}
input[type=submit] {
margin-top: 1em;
}
address { font-size: .75em; text-align: center; }
</style>
<script type="text/javascript" language="javascript">
function init() {
document.url_kw.keyword.focus();
}
</script>
</head>
<body onload="javascript: init();">
<form name="url_kw" action="<?= $_SERVER['PHP_SELF'] ?>" method="get">
<fieldset>
<legend>Where's it rank at Google?</legend>
<? if ($error_message): ?>
<strong class="error_message">Not found in top 100 search results</strong>
<? endif; ?>
<label for="url">URL:</label>
<input type="text" name="url" id="url" size="55" value="<?= isset($_GET['url']) ? $_GET['url'] : 'http://' ?>" />
<br />
<label for="keyword">Keyword:</label>
<input type="text" name="keyword" id="keyword" size="35" value="<?= isset($_GET['keyword']) ? $_GET['keyword'] : null ?>" />
<br />
<input type="submit" name="submit_button" value="SEARCH" onclick="this.value='Searching...';" />
<input type="button" value="CANCEL" onclick="javascript: window.location='<?= $_SERVER['HTTP_REFERER'] ?>';" />
<br />
<p>
<strong>Browser Button</strong>
Drag this link to your browser toolbar, or right-click it and choose Bookmark This Link:
<a href="javascript:location.href='<?= $_SERVER['SCRIPT_URI'] ?>?url='+location.href" title="Where's it rank?" onclick="window.alert('Drag this link to your browser toolbar, or right-click it and choose Bookmark This Link...');return false;">Where's it rank?</a>
</p>
</fieldset>
</form>
</body>
</html>
You can use them in the same page.
Added by roScripts on May-1-2007, 10:27 am
