php移动检测,标题包含手动桌面切换

时间:2012-10-31 14:53:15

标签: php mobile

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

这是我的检测代码:

    <?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;
 }

任何建议表示赞赏。

2 个答案:

答案 0 :(得分:2)

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

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

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

或者只是对这些变量使用非零值,即:http://site.com/test.php/?mobile=1

答案 1 :(得分: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");
}  
?>