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 就足夠了
B. PHP如何識別是電腦端或手機移動端訪問網站
用css來判斷當前頁面的寬度和高度,現在pc的解析度最低都在1200*768以上,iPad pro的寬度為1024px,當寬度大於1100px以上,都可以認為當前訪問端為pc,一般iPad的寬度為768px,iPhone X的寬度為375px;所以當寬度低於800以下的,都視為手機移動端,現在的pc和手機自適應頁面都是按照這個來區分的。
C. php 判斷是否是手機瀏覽器訪問
該功能的實現思路是通過HTTP_X_WAP_PROFILE、HTTP_VIA、HTTP_USER_AGENT等信息來判斷訪客是否通過移動端瀏覽器訪問PHP網站。以下是PHP代碼:
/**
* 是否移動端訪問訪問
*
* @return bool
*/
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;
}
D. ThinkPHP中判斷是手機端還是電腦端訪問的方法
一般是通過訪問的時候請求來源的header頭文件信息來進行判斷的,譬如Android的手機訪問的話一般都會在hedear頭信息裡面帶上Android這樣的信息,同理Iphone、windows phone都會帶。
E. php 驗證訪問瀏覽器是電腦還是手機
最簡單的辦法判斷窗口的寬度:
if(window.innerWidth)
winWidth=window.innerWidth;
elseif((document.body)&&(document.body.clientWidth))
winWidth=document.body.clientWidth;
if(winWidth<=720){
alert("手機屏幕");
}
F. 怎麼用php判斷用戶的訪問是否是來自移動端
functionisMobile()
{
//如果有HTTP_X_WAP_PROFILE則一定是移動設備
if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
{
returntrue;
}
//如果HTTP_VIA含有wap則一定是移動設備,部分服務商會屏蔽該信息
if(isset($_SERVER['HTTP_VIA']))
{
//找不到為flase,否則為true
returnstristr($_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'])))
{
returntrue;
}
}
//協議法,因為有可能不準確,放到最後判斷
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'))))
{
returntrue;
}
}
returnfalse;
}
復制粘貼
G. php 判斷是電腦還是手機瀏覽器打開
$_SERVER['HTTP_USER_AGENT']。這是用來檢查瀏覽頁面的訪問者在用什麼操作系統(包括版本號)瀏覽器(包括版本號)和用戶個人偏好.其中需要檢查用戶的agent字元串,它是瀏覽器發送的HTTP請求的一部分。如:var_mp($_SERVER['HTTP_USER_AGENT']);輸出:string(65)"Mozilla/5.0(WindowsNT6.1;rv:12.0)Gecko/20100101Firefox/12.0"例如:該腳本正常執行之後的輸出是:正在使用InternetExplorer。/view/3909221.htm
H. 在 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"電腦訪問!";
}
?>
I. 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';
}
J. PHP 怎麼判斷是同一個手機訪問
PHP作為基於HTTP運行的伺服器端語言,是無法直接判斷每次訪問的手機是否屬於同一個用戶,加上目前三大手機運營商對請求手機端的號碼的屏蔽,因此無法直接獲得,這個時候可以使用session技術存儲手機端的特殊標志,當然,需要手機端同時開啟cookie存儲對應的session編號。
大概原理就是這樣。