2012-10-31 32 views
0

我正在使用移動偵測腳本來交換頭部包含,但我需要添加一個鏈接以在移動版和桌面版之間切換。php移動偵測,頭部包含手動桌面開關

這裏是我的檢測代碼:

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

我已經試過這種獲取代碼的變化,但我無法弄清楚如何將這些變量傳遞到腳本

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

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

任何意見表示讚賞。

回答

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"); 
} 
?> 
+0

非常感謝你安東尼! – Sean

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

+0

嗨Sammitch,我錯過了什麼可以激活我的代碼後點擊鏈接? – Sean