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