Google Safe Browsing, PHP implementation

News Feed

Google has a database of phishing and malware links that is has turned into hashes, comparing it with the hash of a link can you can identify unsafe links very easily. They call it Google Safe Browsing. Very useful for checking links that maybe submitted to your site in various ways. Recently needing such functionality I wrote a few PHP scripts that enabled me to leverage this powerful system. If you want to use it too, here is all the code you will need to get started.

First you will need to set up a few MySql tables, so here is the needed create table script.

Next you will need to create a file called settings.php that looks something like my example file. You also need a copy of my mysql.class.php file, which I used though several of my sites and hence in this project as well.

To keep the database up to date you need to create a cron task that runs update.safe-browsing.php. Google or ask your host if your not sure how to do this.

Finally you need safe-browsing.class.php which does much of the hard work for you. You can use it as follows.

include "settings.php";
include "mysql.class.php";
include "safe-browsing.class.php";

$url = "http://www.example.com";
$database = new MySqlDatabase(MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD,
    MYSQL_HOST, MYSQL_ERROR_LOG);
$test = new ScamTest($url,$database);
if($test->is_scam()){
    // site is a scam
}
if($test->is_malware()) {
    // site has malware
}
if(cleansite($url,$database)){
    // site is white listed by google
}

If you use any of this code please link back to codefisher.org

Last updated 28th of June 2010 to fix problems in the safe-browsing.class.php.