您知道如何编写检测用户屏幕大小的 PHP 脚本或 Javascript,然后执行 IF 和 else 测试,然后结果将返回特定显示吗?
我在这里要完成的工作:
Javascript 将检测我网站的用户/访问者的分辨率。(我使用媒体查询得到了这个,我在这部分没有问题)
然后脚本将执行 IF 和 ELSE 函数。如果用户的分辨率 = <320x480(手机分辨率),PHP 脚本将调用 Google Adsense - 手机的代码。如果没有,那么它将显示 Google Adsense 网站的代码。
2 个 google adsense 代码保存在两个不同的文件中,我们称之为 adsense-Mobile.php 和 adsense-Website.php
这是一个简单的流程图。
这就是我遇到的问题。许多专家说,在 Jquery 中一个简单的 SHOW-HIDE 元素就可以完成这项工作。不幸的是,就我而言,它没有。
我已经这样做了,它不符合我的要求。问题是来自 google 的两个广告同时加载到网站中,并且只显示了 1 个。Google Adsense 只允许显示 3 个广告。如果我执行显示隐藏 jquery,我将失去产生收入的机会,因为其中一个广告是隐藏的。我需要一些不会加载两个代码但只调用正确的代码的东西(正确的大小,适用于移动设备或网站)。
我知道 PHP 可以做到这一点,但我不知道从哪里开始,或者我应该开始阅读或研究什么函数。非常需要你的帮助。有任何想法吗?任何 PHP 或 javascript 只能调用一个 google adsense 代码,具体取决于分辨率。
这是 google adsense 移动和网站的代码。
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 的代码 - 移动
<?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);
}
?>
非常感谢!我希望我能很好地解释我的问题。