Show Google Adsense code depending on the result of PHP or Javascript IF and ELSE Test

Do you know how to encode a PHP script or Javascript that determines the size of the user's screen, and then execute IF and else test, then the result will return a specific screen?

What I'm trying to do here:

  • Javascript will detect user / visitor permission on my site. (I got it using media queries, I have no problem in this part)

  • then A script will execute the IF and ELSE functions. If user permission = <320x480 (permission for a mobile phone), the PHP script will call codes for Google Adsense - Mobile Phones. Otherwise, it will display the code for Google Adsense sites.

Two Google Adense codes are stored in two different files, allowing you to simply call them adsense-Mobile.php and adsense-Website.php

here is a simple block diagram.

Flow chart

Here lies the problem I encountered. Many experts say that a simple SHOW-HIDE element in jQuery will do the job. Unfortunately, in my case this is not the case.

I have already done this, and it does not meet my requirements. The problem is that two ads from Google are uploaded to the website immediately, and only 1 is filled. Google Adsense only shows 3 ads. If I make show-hide jquery, I will lose the opportunity to generate revenue, because one of the ads is hidden. I need something that does not download two codes, but only calls the correct one (the correct size, for both mobile and web sites).

, PHP , , , . . ? PHP javascript, Adwords Adwords .

Google AdSense Mobile -.

Google Adsense - -

<script type="text/javascript"><!--
google_ad_client = "ca-pub-6099979626157131";
/* Ben Daggers Leader Board */
google_ad_slot = "8829449662";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Google Adsense - MOBILE

<?php

$GLOBALS['google']['client']='ca-mb-pub-6099979626157131';
$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']='8668582435';
$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 % 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) {
    $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;
}

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

?>

! , .

+5
3

php, :

<?php
  if($isMobile){
    include('mobile_file.php');
  }
  else{
    include('website_file.php');
  }
?>
+2

- Javascript:

<head>
<script>
var mediaQuery = "(max-width:480px)", mediaQueryList = window.matchMedia(mediaQuery), isMobile = mediaQueryList.matches;
</script>
<body>
<!-- Your HTML content, build your page, etc. -->

<script>
if (isMobile) {
    // Mobile adsense code
} else {
    //Desktop adsense code
}
</script>
</body>

Javascript , body isMobile - .

0

AdSense AJAX. :

<div id="adsense-box"></div>

, , , AJAX HTML. , jQuery (. Ajax ):

JavaScript:

if ( isMobileResolution ) {
 url = "/mobile_adsense.php";
} else {
 url = "/website_adsesne.php";
}

$.ajax({
 url: url 
}).done(function(content) {
  $("#adsense-box").html(content);
});

, PHP:

Adsense HTML-, Javascript, :

var codemobile = ""; var codewebsite = "";

, HTML . AdSense DOM HTML, google "" - varaibles. " ..

, swich echo $var. = PHP (ob_start() ..). AJAX.

- document.write safe

. , AdSense document.write, , , iframes , iframes src .php .

, AdSense iframe - , - (iframe).

- , /

JavaScript, , cookie , cookie . PHP cookie.

, , . , , cookie.

You can also switch to mobile ads - by default, features are unusual with updating only standard users if this page is viewed more often from mobile devices.

-1
source

All Articles