Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Do you know how to code a PHP script or Javascript that detects the screen size of the user then perform a IF and else test then the result will return a specific display?

What I'm trying to accomplish here:

  1. Javascript will detect the resolution of the user/ visitor of my website. (I got this using media queries, i have no problem on this part)

  2. then A Script will perform a IF and ELSE Function. If the user's resolution is = <320x480 (resolution for mobilephone), the PHP script will call the codes for Google Adsense - Mobile Phones. If not, then it will display the code for Google Adsense Websites.

The 2 google adsense codes are saved in two different files, let's just call them adsense-Mobile.php and adsense-Website.php

here's a simple flowchart.

Flow chart

Here lies the problem i encountered. Many experts says that a simple SHOW-HIDE element in Jquery will do the job. Unfortunately, in my case, it didn't.

I already did that and it doesn't meet my requirements. The problem is the two Ads from google is loaded in the website all at once and only 1 is diplayed. Google Adsense only allow 3 advertisements to be display. If i do the show-hide jquery, i will be loosing the opportunity to generate income since one of the ads is hidden. I need something that will not load the two codes BUT only calls the right one (correct size, either for mobile or websites).

I know PHP can do this, but I don't know where to start or what function should i start to read or research. Need your help badly. Any ideas? Any PHP or javascript that can call only one google adsense codes depending on the resolution.

here's the code for google adsense Mobile and Website.

Code for Google Adsense - WEBSITES

<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>

Code for 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);
}

?>

many Thanks! I hope i explained my question well.

like image 200
Kareen Lagasca Avatar asked Nov 13 '22 08:11

Kareen Lagasca


1 Answers

You can check in php if browser is mobile or not and make:

<?php
  if($isMobile){
    include('mobile_file.php');
  }
  else{
    include('website_file.php');
  }
?>
like image 74
Narek Avatar answered Nov 14 '22 22:11

Narek