0

我正在使用移动检测脚本来交换标头包含,但我需要添加一个链接以在移动版本和桌面版本之间切换。

这是我的检测代码:

    <?php

$mobile_browser = '0';

if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
}

if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
}    

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-');

if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
}

if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
    $mobile_browser++;
}

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
    $mobile_browser = 0;
}

if ($mobile_browser > 0) {
  include("/includes/header_mobile.php");
}
else {

     include("/includes/header_eng.php");
}  
?>

HTML:

<a href="http://site.com/test.php/?mobile">View Mobile Site</a>
<a href="http://site.com/test.php/?full">View Full Site</a>

我已经尝试过这个 GET 代码的变体,但我不知道如何将这些变量传递到脚本中

 if ($_GET['mobile']) {
    $is_mobile = true;
 }

 if ($_GET['full']) {
   $is_mobile = false;
 }

任何建议表示赞赏。

4

2 回答 2

2

由于您的代码当前是这些变量的值,NULL因此相当于falsePHP 中的值。将您的代码更改为:

 if (isset($_GET['mobile'])) {
    $is_mobile = true;
 }

 if (isset($_GET['full'])) {
   $is_mobile = false;
 }

或者简单地为这些变量使用非零值,即:http://site.com/test.php/?mobile=1

于 2012-10-31T14:56:58.430 回答
1

做这样的事情,这将记住用户在会话生命周期内的选择,因此在单击其他链接时不会返回检测代码。

<?php

session_start();

if (isset($_GET['mobile'])) {
    //$is_mobile = true; // don't really need this
    $mobile_browser = 1;
    $_SESSION['force_layout'] = 'mobile';
}
elseif (isset($_GET['full'])) {
    //$is_mobile = false; // don't really need this anymore
    $mobile_browser = 0;
    $_SESSION['force_layout'] = 'full';
}
elseif ($_SESSION['force_layout'] === 'mobile') {
    $mobile_browser = 1;
    $_SESSION['force_layout'] = 'mobile';
}
elseif ($_SESSION['force_layout'] === 'full') {
    $mobile_browser = 0;
    $_SESSION['force_layout'] = 'full';
}
else {

    // run the detection code

    $mobile_browser = '0';

    if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
        $mobile_browser++;
    }

    if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
        $mobile_browser++;
    }    

    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobile_agents = array(
        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
        'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
        'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
        'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
        'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
        'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
        'wapr','webc','winw','winw','xda ','xda-');

    if (in_array($mobile_ua,$mobile_agents)) {
        $mobile_browser++;
    }

    if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
        $mobile_browser++;
    }

    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
        $mobile_browser = 0;
    }

}


// now do your include stuff 

if ($mobile_browser > 0) {
    include("/includes/header_mobile.php");
}
else {
    include("/includes/header_eng.php");
}  
?>
于 2012-10-31T15:39:39.373 回答