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编号。
大概原理就是这样。