Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

mobile version adding of google adsense codes

hi, im not well versed on smarty functions, and i've tried putting the google ad value for mobile in the template but it gets bonk. the sample code is here:


    $GLOBALS['google']['url']=read_global('HTTP_HOST') . read_global('REQUEST_URI');
    $google_dt = time();
    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 % count($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 (count($res_array) == 2) {

    function google_set_muid() {
      $muid = read_global('HTTP_X_DCMGUID');
      if ($muid != '') {
      $muid = read_global('HTTP_X_UP_SUBNO');
      if ($muid != '') {
      $muid = read_global('HTTP_X_JPHONE_UID');
      if ($muid != '') {
      $muid = read_global('HTTP_X_EM_UID');
      if ($muid != '') {

    function google_set_via_and_accept() {
      $ua = read_global('HTTP_USER_AGENT');
      if ($ua == '') {

    function google_get_ad_url() {
      $google_ad_url = '';
      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;

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


Best Answer


  • peregrineperegrine MVP
    edited December 2012


    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • fr3em1ndfr3em1nd ✭✭
    edited December 2012

    hi @peregrine , im sure you're quite aware that mobile themes are written in .tpl ofcourse since the tag is < ? php it's not receiving the codes. there's no visible error, the page is just blank.

    i inserted that code above in the .tpl, im sure this topic could help some newbs around who's wondering how to put a google code with that kind of format.

    i got this resolved and just used { literal } codes here { /literal } and just used the < script > codes from google not < ? php tag mobile ads.

    im just wondering if there's a workaround to make < ? Php tags on mobile, this would be a great help. since the size i want is on < ? php tag served by google.

  • @peregrine as expected you give the topnotch answers here in vanilla forums, thanks.

  • @peregrine as expected you give the topnotch answers here in vanilla forums, thanks.

    thx. insightful

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Hi fr3em1nd:

    I'm also trying to add a Google Adsense mobile ad to the mobile theme.

    You mentioned above: i got this resolved and just used { literal } codes here { /literal } and just used the < script > codes from google not < ? php tag mobile ads.

    What do you mean? I tried using the Google PHP code but removing the opening and closing PHP tags, instead inserting to open and to close, with { literal } and { /literal } . But it's not working for me. The mobile theme is loading properly, i.e. no Bonk, but the add is not appearing. I've added the code to default.master.tpl in themes\mobile\views between {event name="AfterBody"} and the closing tag, thus I'm expecting the adsense ad to appear at the bottom of the page.

    Thanks for any insight you have!

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013


    What he did was insert the Google adsense javascript in the tpl of the theme instead of the php code for google adsense. To be able to add script to a tpl you use

    {literal} Google adsense javascript goes here {/literal} 

    You do not ad php code to the tpl. Insert that js where you need it to appear and use css to adjust it.


    Also look in the plugin info if it has 'MobileFriendly'=TRUE if not add it and see if that makes it appear in the mobile. This has worked for some plugins so it might also work for this if it does not have that already .

  • Hi vrijvlinder:

    Hmmm, Google does not provide javascript as an option when creating Mobile Content Ad Units. They offer 4 options: PHP, CGI/Perl, JSP and ASP. For markup language, the options are 3: xhtml (WAP2.0), wml (WAP1.x) and chtml. I think xhtml (WAP2.0) is correct for Vanilla, ya?

    But after more Adsense research, I see their "normal" content ads, which are provided as javascript, include an ad unit/size called Mobile Banner that is 320x50. So, I created one and placed the javascript between {literal} and {/literal} in the tpl file, before the tag.

    It's working, thank you!

  • I think JSP is JavaServer Pages and the code that Google supplies assumes the site is running on a Java web server, not LAMP architecture, thus it doesn't work (I tried it). For example, the code supplied by Google is (not straight javascript):

    <%@ page import=",,,,,, java.util.ArrayList, java.util.List" %>

    private static final String PAGEAD =

    private void googleAppendUrl(StringBuilder url, String param, String value)
    throws UnsupportedEncodingException {
    if (value != null) {
    String encodedValue = URLEncoder.encode(value, "UTF-8");

    private void googleAppendColor(StringBuilder url, String param,
    String value, long random) {
    String[] colorArray = value.split(",");
    colorArray[(int)(random % colorArray.length)]);

    private void googleAppendScreenRes(StringBuilder url, String uaPixels,
    String xUpDevcapScreenpixels, String xJphoneDisplay) {
    String screenRes = uaPixels;
    if (screenRes == null) {
    screenRes = xUpDevcapScreenpixels;
    if (screenRes == null) {
    screenRes = xJphoneDisplay;
    if (screenRes != null) {
    String[] resArray = screenRes.split("[x,*]");
    if (resArray.length == 2) {

    private void googleAppendMuid(StringBuilder url, List muids) {
    for (String muid : muids) {
    if (muid != null) {

    private void googleAppendViaAndAccept(StringBuilder url, String via,
    String accept) throws UnsupportedEncodingException {
    googleAppendUrl(url, "via", via);
    googleAppendUrl(url, "accept", accept);


    long googleDt = System.currentTimeMillis();
    StringBuilder googleAdUrlStr = new StringBuilder(PAGEAD);
    googleAppendUrl(googleAdUrlStr, "ip", request.getRemoteAddr());
    googleAppendUrl(googleAdUrlStr, "ref", request.getHeader("Referer"));
    String googleUrl = request.getRequestURL().toString();
    if (request.getQueryString() != null) {
    googleUrl += "?" + request.getQueryString().toString();
    googleAppendUrl(googleAdUrlStr, "url", googleUrl);
    String googleUserAgent = request.getHeader("User-Agent");
    googleAppendUrl(googleAdUrlStr, "useragent", googleUserAgent);
    googleAppendScreenRes(googleAdUrlStr, request.getHeader("UA-pixels"),
    List googleMuids = new ArrayList();
    googleAppendMuid(googleAdUrlStr, googleMuids);
    if (googleUserAgent == null || googleUserAgent.length() == 0) {
    googleAppendViaAndAccept(googleAdUrlStr, request.getHeader("Via"),
    try {
    URL googleAdUrl = new URL(googleAdUrlStr.toString());
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(googleAdUrl.openStream(), "AUTO_DETECT"));
    for (String line; (line = reader.readLine()) != null;) {
    } catch (IOException e) {}


  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    lol indeed , well at least there was another option and it worked...I edited the super long info about servlets and how this uses xml tags to encapsulate the logic, but it's there in the oracle website. This stuff should run on anything according to the info. But mobile , yea that is a stretch maybe.

Sign In or Register to comment.