Home

How to have multiple mobile ads with php 5.3

I implemented Google ads with analytics on the mobile version of this site. To display ads for all mobile phones Google gives you server-side code to copy and paste into your website. I chose to get the code in php. I was surprised to find that the code threw several notices and wasn't as efficient as it could be. It seems as though this code wasn't written for php 5.3 or to have more than one ad on the same page.

Google tells you to paste some code into your site everywhere you want an ad. This code starts by setting a bunch of global variables and then defines a ton of functions. This code contains a depreciated function twice, a formatting error and only needs to be included once. The only code that needs to be included more than once is this tiny little snippet:

<?php  
$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);
}
?>

The depreciated function I'm referring to is split. It can be replaced with explode in its first occurrence which is in the function google_append_color. The second time it appears is in the function google_set_screen_res where it needs to be replaced with preg_split.

The formatting error is in the function google_get_ad_url where there's a line break after "'dt'," on the second line. You can simply remove the carriage return and it should work fine. Here's the complete fixed version of the code they give you with my personal information removed:

<?php  
$GLOBALS['google']['client']='xx-xx-xxx-5555555555555555';
$GLOBALS['google']['https']=read_global('HTTPS');
$GLOBALS['google']['ip']=read_global('REMOTE_ADDR');
$GLOBALS['google']['markup']='xhtml';
$GLOBALS['google']['output']='xhtml';
$GLOBALS['google']['ref']=read_global('HTTP_REFERER');
$GLOBALS['google']['slotname']='5555555555';
$GLOBALS['google']['url']=read_global('HTTP_HOST') . read_global('REQUEST_URI');
$GLOBALS['google']['useragent']=read_global('HTTP_USER_AGENT');
$google_dt = time();
google_set_screen_res();
google_set_muid();
google_set_via_and_accept();
       
function read_global($var) {
    return isset($_SERVER[$var]) ? $_SERVER[$var]: '';
}

function google_append_url(&$url, $param, $value) {
    $url .= '&' . $param . '=' . urlencode($value);
}

function google_append_globals(&$url, $param) {
    google_append_url($url, $param, $GLOBALS['google'][$param]);
}

function google_append_color(&$url, $param) {
    global $google_dt;
    $color_array = explode(',', $GLOBALS['google'][$param]);
    google_append_url($url, $param,$color_array[$google_dt % sizeof($color_array)]);
}

function google_set_screen_res() {
    $screen_res = read_global('HTTP_UA_PIXELS');
    if ($screen_res == '') {
        $screen_res = read_global('HTTP_X_UP_DEVCAP_SCREENPIXELS');
    }
    if ($screen_res == '') {
        $screen_res = read_global('HTTP_X_JPHONE_DISPLAY');
    }
    $res_array = preg_split('[x,*]', $screen_res);
    if (sizeof($res_array) == 2) {
        $GLOBALS['google']['u_w']=$res_array[0];
        $GLOBALS['google']['u_h']=$res_array[1];
    }
}

function google_set_muid() {
    $muid = read_global('HTTP_X_DCMGUID');
    if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
        return;
    }
    $muid = read_global('HTTP_X_UP_SUBNO');
    if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
        return;
    }
    $muid = read_global('HTTP_X_JPHONE_UID');
    if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
        return;
    }
    $muid = read_global('HTTP_X_EM_UID');
    if ($muid != '') {
        $GLOBALS['google']['muid']=$muid;
        return;
    }
}

function google_set_via_and_accept() {
    $ua = read_global('HTTP_USER_AGENT');
    if ($ua == '') {
        $GLOBALS['google']['via']=read_global('HTTP_VIA');
        $GLOBALS['google']['accept']=read_global('HTTP_ACCEPT');
    }
}

function google_get_ad_url() {
    $google_ad_url = 'http://pagead2.googlesyndication.com/pagead/ads?';
    google_append_url($google_ad_url, 'dt',round(1000 * array_sum(explode(' ', microtime()))));
    foreach ($GLOBALS['google'] as $param => $value) {
        if (strpos($param, 'color_') === 0) {
            google_append_color($google_ad_url, $param);
        } else if (strpos($param, 'url') === 0) {
            $google_scheme = ($GLOBALS['google']['https'] == 'on')
                    ? 'https://' : 'http://';
            google_append_url($google_ad_url, $param,
                                                $google_scheme . $GLOBALS['google'][$param]);
        } else {
            google_append_globals($google_ad_url, $param);
        }
    }
    return $google_ad_url;
}
?>

Their mobile analytics code is much better but it generates a notice if no referrer is set. To fix this issue all you need to do is do an if(isset()) on $_SERVER["HTTP_REFERER"]. The modified analytics code looks like this:

<?php  
// Copyright 2009 Google Inc. All Rights Reserved.
$GA_ACCOUNT = "XX-5555555-5";
$GA_PIXEL = "/ga.php";
function googleAnalyticsGetImageUrl() {
    global $GA_ACCOUNT, $GA_PIXEL;
    $url = "";
    $url .= $GA_PIXEL . "?";
    $url .= "utmac=" . $GA_ACCOUNT;
    $url .= "&utmn=" . rand(0, 0x7fffffff);
    if(isset($_SERVER["HTTP_REFERER"])) {
        $referer = $_SERVER["HTTP_REFERER"];
    }
    else {
        $referer = '';
    }
    $query = $_SERVER["QUERY_STRING"];
    $path = $_SERVER["REQUEST_URI"];
    if (empty($referer)) {
        $referer = "-";
    }
    $url .= "&utmr=" . urlencode($referer);
    if (!empty($path)) {
        $url .= "&utmp=" . urlencode($path);
    }
    $url .= "&guid=ON";
    return str_replace("&", "&amp;", $url);
}
?>
Share/Bookmark