php判斷是手機訪問
//php載入函數要比載入類要快一些,建議使用函數判斷
/**
*判斷是否是通過手機訪問
*
*/
function isMobile() {
// 如果有HTTP_X_WAP_PROFILE則一定是移動設備
if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true;
}
//如果via信息含有wap則一定是移動設備,部分服務商會屏蔽該信息
if (isset ($_SERVER['HTTP_VIA'])) {
//找不到為flase,否則為true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
//判斷手機發送的客戶端標志,兼容性有待提高
if (isset ($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array (
'nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap',
'mobile'
);
// 從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
//協議法,因為有可能不準確,放到最後判斷
if (isset ($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml並且不支持html那一定是移動設備
// 如果支持wml和html但是wml在html之前則是移動設備
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false)
&& (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false ||
(strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') <
strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}
② php 怎樣判斷用戶使用的是手機瀏覽器
$_SERVER['HTTP_USER_AGENT']。
這是用來檢查瀏覽頁面的談羨渣訪問者在用什麼操作系統(包括版本號)瀏覽器(包括版本號)和用戶個人偏好.
其中需要檢查用戶的 agent 字元串,它是瀏覽器發送的 HTTP 請求的一部分。
如:var_mp($_SERVER['HTTP_USER_AGENT'派凳]);
輸出:string(65) "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0"
例如:
<?phpif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) { echo '正在使用 Internet Explorer。<br />';}?>
該腳本正常執行之後的輸出是:
正在使含悄用 Internet Explorer。
http://ke..com/view/3909221.htm
③ PHP判斷是手機端訪問還是PC端訪問網站
主要是通過$_SERVER ['HTTP_USER_AGENT']來判斷的
自己整理的一個方法,代碼如下
/**
*是否是手機訪問,true是,false否
*@returnboolean
*/
publicstaticfunctionisMobile(){
$mobilebrowser_list=array(
'iphone',
'android',
'phone',
'wap',
'netfront',
'java',
'operamobi',
'operamini',
'ucweb',
'windowsce',
'symbian',
'series',
'webos',
'sony',
'blackberry',
'dopod',
'nokia',
'samsung',
'palmsource',
'xda',
'pieplus',
'meizu',
'midp',
'cldc',
'motorola',
'foma',
'docomo',
'up.browser',
'up.link',
緩帆 'blazer',
'helio',
'hosin',
'huawei',
'novarra',
'coolpad',
'webos',
'techfaith',
'palmsource',
'alcatel',
'amoi',
'ktouch',
'nexian',
'ericsson',
'philips',
'sagem',
'wellcom',
'bunjalloo',
'maui',
'smartphone',
'iemobile',
'spice',
'bird',
'zte-',
'longcos',
'pantech',
'gionee',
'portalmmm',
'jigbrowser',
'hiptop',
手悶 'benq',
'haier',
'^lct',
'320x320',
'240x320',
'176x220'
);
$useragent=strtolower($_SERVER['HTTP_USER_AGENT']擾薯雹);
$mobile_change=false;
if(!empty($useragent)){
foreach($mobilebrowser_listas$v){
if(stristr($useragent,$v)!==false){
returntrue;
}
}
}
returnfalse;
}
④ php如何實現判斷是不是手機訪問而轉跳到wap
1
2
3
4
5
6
7
8
9
10
11
if ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad'虧畢) === false)
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle'銷猛芹) !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
echo '是手機訪問'知升;
} else {
echo '不是手機訪問';
}
⑤ PHP CI 框架 怎麼在訪問的是否區分手機 和 電腦訪問
在控制層可以進行判斷,代碼蠢神如下:(挖扮知個墳)
//載入類庫
$this->load->library('user_agent');
//判斷是否是手機端登錄
if ($this->agent->is_mobile())
{
//是則跳轉這個網頁
$this->load->view('phone.php');
}
else
{
不是則跳轉廳檔消這個網頁
$this->load->view('pc.php');
}
⑥ PHP 怎麼識別手機訪問
網上現在有三種觀點, 一種是基於瀏覽器發送的 User-Agent, 但明顯這種方法是行不通的, 雖然有人列出了大多數的手機發送的User-Agent, 但依然會有很多手機無法識別, 甚至有些手機瀏覽器不發送User-Agent, 而且也不能保證以後就不會出新牌子.
下面是一個php的例子, 看過之後你就會知道有多麼不可靠.
1. function is_wap(){
2. $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
3. $uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|sie|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile)/i";
4. if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap')){
5. return true;
6. }else{
7. return false;
8. }
9. }
另外一種是判斷HTTP_ACCEPT, 這種應該相對可靠一點, 不過HTTP_ACCEPT是非常復雜的, 如果一般的不支持html的瀏覽器還好說, 你只要判斷瀏覽器支持wml並且不支持html就可以, 但如果瀏覽器同時支持wml和html那就難辦了, 低端手機可能會將wml放到html的前面, 但是很多高端手機或者智能手機對html的支持很好, 所以html也會出現在wml前面.
這是黑莓手機發送的HTTP_ACCEPT
application/vnd.rim.html,
text/html,
application/vnd.wap.xhtml+xml,
text/vnd.sun.j2me.app-descriptor,
image/vnd.rim.png,image/jpeg,
application/x-vnd.rim.pme.b,
application/vnd.rim.ucs,
image/gif;anim=1,
application/vnd.rim.jscriptc;v=0-8-8,
application/x-javascript,
application/vnd.rim.css;v=1,
text/css;media=handheld,
application/vnd.wap.wmlc;q=0.9,
application/vnd.wap.wmlscriptc;q=0.7,
text/vnd.wap.wml;q=0.7,
*/*;q=0.5
誇張吧? 不過的確是很標準的, 開發人員可以根據這個得到很多信息, 但很多手機都沒這么標準的, 如果你查看IE或者FF的HTTP_ACCEPT你會發現它很短.
text/html,
application/xhtml+xml,
application/xml;q=0.9,
*/*;q=0.8
同樣, 給你個php的例子.
if (isset($_SERVER['HTTP_ACCEPT']) &&
(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==FALSE)
&& (strpos($_SERVER['HTTP_ACCEPT'],'text/html')===FALSE ||
(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml') <
strpos($_SERVER['HTTP_ACCEPT'],'text/html'))
)) {//手機訪問
readfile('index.wml');
} else readfile('index.htm');
原理:如果瀏覽器支持WML、而且[不支持HTML]或[WML優先於HTML],則判定為手機。但實際上這個原理是不正確的.
還有一種是說判斷網關或者IP什麼的, 但我個人認為這個很不可靠, 手機的聯網方式很多, 比如我開發的時候就是用模擬器的.這個應該不用什麼例子了, REMOTE_ADDR 就足夠了
⑦ 在 PHP 開發中如何判斷用戶是否使用手機
PHP通過內置全局變數$_SERVER['HTTP_USER_AGENT']來獲取用戶信息,包括瀏覽器信息,操作系統等;判斷是否是手機還是電腦終端訪問,只需判斷他的$_SERVER['HTTP_USER_AGENT']信息是否存在手機終端類型即可。示例如下:
<?php
//1.獲取用戶瀏覽器信息等;
$user_agent=$_SERVER['HTTP_USER_AGENT'];
//2.列舉常用手機終端類型
$mobile_agents=array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi",
"android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio",
"au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu",
"cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly",
"fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi",
"htc","huawei","hutchison","inno","ipad","ipaq","iphone","ipod","jbrowser","kddi",
"kgt","kwc","lenovo","lg","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo",
"mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-",
"moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia",
"nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-",
"playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo",
"samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank",
"sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit",
"tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin",
"vk-","voda","voxtel","vx","wap","wellco","wigbrowser","wii","windowsce",
"wireless","xda","xde","zte");
foreach($mobile_agentsas$device){
if(stristr($user_agent,$device)){
//手機訪問
$is_mobile=true;
break;
}
}
//判斷手機標識是否為真
if($is_mobile){
echo"手機訪問!";
}else{
echo"電腦訪問!";
}
?>
⑧ PHP 怎麼判斷是同一個手機訪問
PHP作為基於HTTP運行的伺服器端語言,是無法直接判斷每次訪問的手機是否屬於同一個用戶,加上目前三大手機運營商對請求手機端的號碼的屏蔽,因此無法直接獲得,這個時候可以使用session技術存儲手機端的特殊標志,當然,需要手機端同時開啟cookie存儲對應的session編號。
大概原理就是這樣。
⑨ PHP如何識別是電腦端或手機移動端訪問網站
用css來判斷當前頁面的寬度和高度,現在pc的解析度最低都在1200*768以上,iPad pro的寬度為1024px,當寬度大於1100px以上,都可以認為當前訪問端為pc,一般iPad的寬度為768px,iPhone X的寬度為375px;所以當寬度低於800以下的,都視為手機移動端,現在的pc和手機自適應頁面都是按照這個來區分的。
⑩ php中怎樣實現判斷是手機訪問還是電腦瀏覽器訪問
嚴格的講strpos($_SERVER['HTTP_ACCEPT'],'wap')!==false 判斷是不夠的,網上找了段比較全的測試無誤,貼出來mark下:
/**
*判斷是手機訪問pc訪問
*/
protectedfunctioncheck_wap(){
if(isset($_SERVER['HTTP_VIA'])){
returntrue;
}
if(isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])){
returntrue;
}
if(isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])){
returntrue;
}
if(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML")>0){
//Checkwhetherthebrowser/gatewaysaysitacceptsWML.
$br="WML";
}else{
$browser=isset($_SERVER['HTTP_USER_AGENT'])?trim($_SERVER['HTTP_USER_AGENT']):'';
if(empty($browser)){
returntrue;
}
$mobile_os_list=array('GoogleWirelessTranscoder','WindowsCE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','OperaMobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
$found_mobile=$this->checkSubstrs($mobile_os_list,$browser)||$this->checkSubstrs($mobile_token_list,$browser);
if($found_mobile){
$br="WML";
}else{
$br="WWW";
}
}
if($br=="WML"){
returntrue;
}else{
returnfalse;
}
}
/**
*判斷手機訪問,pc訪問
*/
protectedfunctioncheckSubstrs($list,$str){
$flag=false;
for($i=0;$i<count($list);$i++){
if(strpos($str,$list[$i])>0){
$flag=true;
break;
}
}
return$flag;
}
if($this->check_wap()){
echo'wap';
}else{
echo'pc';
}