AdSense for Mobile Woes – Solved!

AdSense LogoRemember a month ago when I blogged about how AdSense for Mobile had suddenly stopped working on wapreview.mobi.  And my repeated attempts at contacting Google using their official support email and online help form and getting nothing in reply except unhelpful automated replies?

Well I finally got AdSense working again, no thanks to Google and their apparent policy of no support for AdSense.  To be fair, the problem was never Google’s.  It turned out that something I did broke AdSense.  That something was upgrading from PHP4 to PHP5!   I wanted to use htmLawed, a  library that requires PHP5, in the new mobile version of the WapReview Directory. Although I made no changes to the Google AdSense code, the simple act of upgrading PHP caused AdSense to stop working.  That’s because  I use  1and1 shared hosting and 1and1 has set the allow_url_fopen flag in php.ini to false for PHP5 and true for PHP4. Google uses fopen() to retrieve ads from a remote server in their domain.  There are some real security issues around using fopen() particularly if you use it with  user specified urls. The way Google uses fopen() with a single hard-coded url is perfectly safe.  Still I can’t blame 1and1 for disabling the functionality as allowing fopen() with remote urls  is a security risk in a shared hosting environment.  You’d  think they would be consistent though and disable allow_url_fopen on both supported PHP versions.

If you are using AdSense for Mobile with 1and1 or another host that has disabled allow_url_fopen you can probably get AdSense to work with a simple modification to the Google supplied PHP code.  You need to replace the fopen() call with calls to cURL functions. Look for the following in your AdSense code  (it’s at the very bottom of the Google supplied code).

$google_ad_handle = @fopen(google_get_ad_url(), 'r');
if ($google_ad_handle) {
  while (!feof($google_ad_handle)) {
    echo fread($google_ad_handle, 8192);
  }
  fclose($google_ad_handle);
}
?>

Replace the above  code snippet with:

function curl_get_contents($url, $timeout=60) {
  $timeout = abs((int)$timeout);
  $curl = curl_init();
  $opts = array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => $timeout
  );

  curl_setopt_array($curl, $opts);
  $result = curl_exec($curl);
  if($result === false){
    user_error(curl_error($curl));
    return false;
  }
  $response = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  if($response != 200){
    user_error("Received HTTP response of $response.");
  }
  return $result;
}

$url= google_get_ad_url();
$contents = curl_get_contents($url);
echo $contents;
?>

The curl_get_contents() function used above is from user NogDog at the PHPBuilder.com forum.

Disabling the remote url functionality is apparently quite common in shared hosting environments.  So why does Google persist in using it in the PHP code?  Simple, the cURL library is not enabled by default  in any PHP version.  I don’t have a clue why not as it’s the preferred, safer way to open remote files.  Because curl is an optional PHP extension it really isn’t possible to provide a single code snippet that works in all environments.  Still it would be nice of Google to warn users about this potential issue and offer an alternate version of their code using cURL .  That’s  what AdMob does.  They provide two versions of their PHP code; one using fopen() and the other cURL. AdMob recomends trying the cURL code first and if that doesn’t work then using the fopen() variant.

6 thoughts on “AdSense for Mobile Woes – Solved!

  1. Hi, please can u give me the complete adsense mobile code. Thanks. Post it here so that other googlers can see it.

    • I switched Web hosts and my new hosting provider doesn’t block fopen() so I no longer use or have a copy of the modified code. You will have to follow the instructions in the post to modify the Adsense code yourself.

  2. Thanks for the post.I was also facing the same problem and thought google has banned me or something like that.Then i tried ur workaround the ads reappeared then again i switched back to old code.Also allow_url_fopen is off in my server.

    Does changing code account to policy violation with adsense ?

Comments are closed.