logo logo

Script to check if a domain is banned from Adsense program or not

I want to create this kind of script for a long time a go, inspired by Digitalpoint Adsense sandbox tool. Since I have quite a free time now, I decided to do some little research on how to create an Adsense sandbox script. Supposedly to check whether a domain is banned from Google Adsense program or not. After a little research (trial and error), actually this kind of script is not so hard to implement (i.e. once you know the correct URL to call :P ).

So, here I create another PHP library that will check whether a domain (the domain name, not the user Adsense account)  is banned or not. Here’s the full library source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//helper function
//file_get_contents using curl
function file_get_contents_curl($url, $referer="")
    {
    $header_list=array('Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
	'Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
	'Accept-Language: en-US,en;q=0.8');
 
	$ualist=array(
	'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)',
	'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.23 (KHTML, like Gecko) Ubuntu/10.04 Chromium/11.0.688.0 Chrome/11.0.688.0 Safari/534.23',
	'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110221 Ubuntu/10.04 (lucid) Firefox/3.6.14 GTB7.1',
	'Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.01',
	'Midori/0.2.2 (X11; Linux i686; U; en-us) WebKit/531.2+',
	'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0',
	'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1150.1 Iron/20.0.1150.1 Safari/536.11'
	);
 
	$ch = curl_init();
	//curl_setopt($ch, CURLOPT_HEADER, 0); //not requesting header
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //Set curl to return the data instead of printing it to the browser.
    if ($referer!="") curl_setopt($ch, CURLOPT_REFERER, $url);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_USERAGENT, $ualist[rand(0, count($ualist)-1)]);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header_list);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_TIMEOUT,60);
	$data = curl_exec($ch);
	curl_close($ch);
 
	return $data;
}
 
//another helper function
function cut_str($str, $left, $right) {
    $str = substr ( stristr ( $str, $left ), strlen ( $left ) );
    $leftLen = strlen ( stristr ( $str, $right ) );
    $leftLen = $leftLen ? - ($leftLen) : strlen ( $str );
    $str = substr ( $str, 0, $leftLen );
    return $str;
}
 
//main function
function is_banned($url, $ad_type='text', $format='160x600') {
	$base_url='http://pagead2.googlesyndication.com/pagead/ads?adtest=on&client=ca-skdhfksdhfksdjksjd&random='.rand(1000, 10000000).'&url='.urlencode($url).'&gl=default&ad_type='.$ad_type.'&format='.$format;
 
	$raw=file_get_contents_curl($base_url);
 
	if (strpos($raw, 'id=ads')===FALSE) {
		return TRUE; //is truly banned from Adsense
	} else {
		//get advertisers
		$ads_part=explode('<li', cut_str($raw, '<ul', '</ul>'));
		//var_dump($ads_part);
		$advertisers=array();
		for($i=1;$i<=(count($ads_part)-1);$i++){
			$advertisers[$i-1]=array(
				'url'=>cut_str($ads_part[$i], 'adurl=', '"'),
				'title'=>htmlspecialchars_decode(cut_str($ads_part[$i], '<span>', '</span>')),
				'desc'=>htmlspecialchars_decode(strip_tags(cut_str($ads_part[$i], '<div class=adb >', '</div>')))
			);
		}
 
		return $advertisers;
	}
}

It’s a very basic PHP code without regular expression with a little cURL added. I’m sure you can improve it. :D

How to use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ads=is_banned("mp32u.net");
 
//check if banned
if (!is_array($ads)) {
    //woops the domain is BANNED
    //do something here
} else {
    //it's not banned
    //you can show possible advertisers URL
    for ($i=0; $i<=count($ads)-1; $i++) {
        //loop through $ads
        //do something, can be shown to table or something
        //$ads[$i]['url'];
        //$ads[$i]['title'];
        //$ads[$i]['desc'];
    }
}  //end if

The function will return an array of possible advertisers for the input URL. If the domain is banned, the function will return TRUE, if not banned, it will return an array.

Example array of advertisers result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
array(2) {
  [0]=>
  array(3) {
    ["url"]=>
    string(28) "http://www2.itt-tech.edu/mgs"
    ["title"]=>
    string(24) "ITT Tech - Official Site"
    ["desc"]=>
    string(62) "Convenient Schedules, Over 130 Locations. Browse New Programs."
  }
  [1]=>
  array(3) {
    ["url"]=>
    string(532) "http://537.xg4ken.com/media/redir.php%3Fprof%3D78%26camp%3D1325%26affcode%3Dcr654548%26cid%3D12788267592%7C491716%7C%26mType%3D%26networkType%3Dcontent%26url%5B%5D%3Dhttp%253A%252F%252Finformation.keller.edu%252Fus%252Fmba-cpa%253Fvc%253D166148%2526agid%253D0078x28874%2526cmpid%253Dctd-Keller-GG%2526utm_source%253Dgoogle%2526utm_medium%253Dcpc%2526utm_term%253D%2526utm_content%253D12788267592%2526utm_campaign%253DAccounting-%252520Content_VC%25253D166148_MT%25253DCTP%2526sc_1%253D0078S001325GGCTP%2526sc_2%253D_kenshoo_clickid_"
    ["title"]=>
    string(22) "Keller Graduate School"
    ["desc"]=>
    string(67) "Prepare For Success w/ Keller&#39;s Accounting Master&#39;s Degree."
  }
}

Fully working demo can be tested here: http://demo.ahowto.net/adsense_lib/
check if domain is banned by adsense

Close enough to Digitalpoint Adsense Sandbox tool.. :P

bottom

2 Responses to “Script to check if a domain is banned from Adsense program or not”

  1. johnny says:

    Hi thanks for sharing this. Can you please provide a few more details on how to put this on another site?

    Thanks for your time

    Johnny

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bottom