Google SERPS

18 01 2009

We have all wanted to be able to look through Google’s search results anywhere from 50-100 results and see how far out our website is for a particular term, or keyword. We just don’t want to go through pages and pages, and what if we happen to miss our page…..it’s a big pain.

I developed this tool in about an hour, maybe less, to shorten the pain. I have to tell you though, as Google sometimes has extra links and such in their results that the results are not always 100% accurate, not that it matters too much, but I just wanted to let you know. The script depends on a pattern within Googles results, but Googles search results are messed up - tags that don’t have closed counterparts, etc. It is a pain. So without further ado, here is my Curl PHP Google SERPS script.

Check out my working example first

serps.php

<?php

// no need for sessions or database in this script. Just my normal Curl function.

$date = date("U"); // Unix time stamp...

// The wonderful Curl function that lets us grab a page and save it in a variable.
function google_serps($url,$post_info){

// absolute path and file name of the cookie file
$cookie_file = "/home/iluvjohn/public_html/email/cookie.txt";

// we are always going to use cookies. No need to change this very often:
$usecookie = true;

// this is always going to be blank? But if we want we can set it to something
$refer = "http://iluvjohn.com";

// if we are going to use cookies, then check to see if we can write to the file.
if ($usecookie == true){
if (file_exists($usecookie)) {
if (!is_writable($usecookie)){
return "Can't write to $cookie_file cookie file, change file permission to 777 or remove read only for windows.";
}else{
$handle = fopen($cookie_file, 'w') or die("can't open file");
fclose($handle);
if(!is_writable($cookie_file)){return "Can't write to $cookie_file cookie file, change file permission to 777 or remove read only for windows.";}
}
}
}

// Lets get down to Curl Business
// Initiate the Curl Function
$ch = curl_init();

// the url we will visit
curl_setopt($ch, CURLOPT_URL, $url);

// verify ssl peer?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

// if we added some info to the second fiel in the function, $post_info, then we will send it as post information
// post information is the same way variables in the URL are put out:
// value1=someinfo&name=John&music=goodstuff
if(!empty($post_info)){
// post information to be sent
curl_setopt ($ch, CURLOPT_POSTFIELDS, $use_post);
}
// not all pages we view will use SSL, but it is ok to leave it on anyways. If you get any errors, just comment it out.
// verify ssl host?
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

// set header flag
curl_setopt($ch, CURLOPT_HEADER, 1);

// pass what browser we are using, this can say anything....
// if you use Curl to make your own bot you could set it
// to something like ILuvJohn Bot
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

// if we are going to use cookies
if ($usecookie == true) {
// set cookie session
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
// set cookie jar (the file we are going to use)
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
// the cookie file - same as cookie jar? ...This has been working for me
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
}

// set referer
if (!empty($refer)){curl_setopt($ch, CURLOPT_REFERER, $refer );}

// tell curl to return the page we get in responce
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

// execute the curl commands and save the returned page / results in a variable for use later on
$result = curl_exec($ch);

// close curl connection with the remote server
curl_close ($ch);

// return our page!
return $result;
}

echo "<h1>Google Search Engine Page Rank</h1>"; // heading

$query = $_POST['query']; // keywords that we will search for
$domain = $_POST['domain']; // the domain we will be searching for

if(empty($domain)){$domain = "default_domain.com";} // let's set a default domain to save us some time....if we want to.

// our search form, put it in a function for easy access.
// We just make sure that we global our $query and $domain!
function sform(){
global $query,$domain;
echo "<form action=\"serps.php\" method=\"post\">";
echo "Your Search Terms: <input type=\"text\" name=\"query\" value=\"$query\" />";
echo "Your Domain: (http|https|ftp)://<input type=\"text\" name=\"domain\" value=\"$domain\" />";
echo "<input type=\"submit\" name=\"doserps\" value=\"Find Your Google SERP\" />";
echo "</form>";
}

sform(); // Put the form on the screen

echo "<br /><br />";

// So if someone submits to the page, we want to get the results and display them.....
if(isset($_POST['doserps'])){

$num = 100; // This is how far down Google's listings do we want to search.... 100 or 200 is usually pretty good....

//I am not sure how far Google with go though. Also, the bigger the number here, the longer it takes to load the page...if it is too big, the whole thing will time out.....becareful.
$query = urlencode($query); // Let's get our Query ready for Google...

$url = "http://www.google.com/search?hl=en&safe=off&q=$query&btnG=Search&num=$num"; // this is the url we are going to scrape and take apart....it contains our query and how many results we want back

$page = google_serps($url,$post_info); // use my curl function to grab the page.

// explode the pages at the li, all the results are now seperated

$page = str_replace("<ol>","",$page); // get rid of some extra stuff

$pages2 = explode("<li class=g>",$page); // break the page up again into even more pieces....

// count = 0
$count = 0;

// parse through each search result
foreach ($pages2 as $k2 => $v2){
if($count != 0){
$pages = explode("<li class=\"g w0\">",$v2);
//$pages[0] = "";
foreach ($pages as $k => $v){

$v = str_replace("<h3>","",$v);
$v = str_replace("</li>","",$v);

$br = explode("</h3>",$v,$vs); //

eregi("(http|https|ftp)://[www.]{0,}$domain+[a-zA-Z0-9!-~]{0,}",$br[0],$u); // look for our domain!

$result = $u[0];

// we use $echo as a variable to append to and carry out our message to the bottom of the screen

$echo .= $count.") "; // what number we are at
if(!empty($result)){
$success++;
$echo .= "<strong><a href=\"$result\" target=\"_blank\">".str_replace("\"","",$result)."</a></strong>";

$res .= "#"."$count<br />";

}else{
eregi('"(http|https|ftp)://[a-zA-Z0-9!-~]{0,}"',$v,$u); // look for our domain
$echo .= str_replace("\"","",$u[0]);
}

$echo .= "<br />";
unset($result);
unset($u);
unset($br);
unset($vs);
// unset some stuff! Very important!

}
}
$count++; // +1 to the count
}

}else{

// if no search, direct the user to make a search
$echo .= "Please make a search to find your Google SERPS!<br />";

}

$res = str_replace("class=g>","",$res); // get rid of some extra stuff

if(!empty($_POST)){
if(!empty($res)){ // echo success results
echo "Your URL appeared <b>$success</b> time";
if($success != 1){echo "s";}
echo " at the following position";
if($success != 1){echo "s";}

echo ":<br /><b>$res</b><br /><br />";
echo $echo; //echo the results

}else{

echo "Your site did not show up at all. Keep at it! You will get it soon!"; // no result message

}
}else{

echo "Please make a search above!<br />"; // direct user to make a search

}

?>

So here we have the whole thing. It is pretty self explainitory.  Feel free to use my example script here on my server if you would like, I dont mind.

I ask that if you make any improvements to please let me know! I will give credit and as many backlinks as I can! Plus it would be nice to have for myself!

Please comment below!

Written by: John Minton



Windows XP Auto Login - Registry Entry

18 01 2009

The following Registry Entry will allow windows to login a particular account upon booting up. The labels are self explanatory.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"DefaultUserName"="Administrator"
"defaultpassword"="password"
"autoadminlogon"="1"

DefaultUserName is the username of the account. Above we used Administrator
defaultpassword is the password that windows will use.
autoadminlogon must be set to “1″ so that windows knows to try and log in automatically with the default user/pass specified previously.

Save this to a .reg file, edit it to match a user on your loca machine, then run it and restart your computer. It should automatically login to the correct account!

Written by: John Minton



Windows Logon Message - Registry Entries

18 01 2009

Here we are going to modify the Registry to show us a caption when turning on. This will happen at the login screen, before logging in I believe…..but I may be wrong.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"LegalNoticeCaption"="Title"
"LegalNoticeText"="Message."

Save this entire chunk of text to notepad, and save it as a .reg file. Then double click on it. It should ask you if you would like to add the information to your Registry. Click yes, then click ok after it is done! If you restart you should see this message somewhere along the way with an ok button.

Written by: John Minton