很多站长朋友们都不太清楚php判断pc,今天小编就来给大家整理php判断pc,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP判断是手机端访问还是PC端访问网站 2、 php判断是移动端还是pc 3、 php如何判断客户端是pc还是手机 4、 php如何判断页面是手机端还是pc端 5、 php判断客户端是pc还是手机 PHP判断是手机端访问还是PC端访问网站主要是通过$_SERVER ['HTTP_USER_AGENT']来判断的
自己整理的一个方法,代码如下
/**
* 是否是手机访问 ,true是,false否
* @return boolean
*/
public static function isMobile() {
$mobilebrowser_list = array (
'iphone',
'android',
'phone',
'wap',
'netfront',
'java',
'opera mobi',
'opera mini',
'ucweb',
'windows ce',
'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',
'jig browser',
'hiptop',
'benq',
'haier',
'^lct',
'320x320',
'240x320',
'176x220'
);
$useragent = strtolower ( $_SERVER ['HTTP_USER_AGENT'] );
$mobile_change = false;
if (! empty ( $useragent )){
foreach ( $mobilebrowser_list as $v ){
if (stristr( $useragent, $v ) !== false){
return true;
}
}
}
return false;
}
php判断是移动端还是pc从$_SERVER的HTTP_USER_AGENT变量值的不同进行判断
移动端有移动端的值,PC端有PC端的值
php如何判断客户端是pc还是手机//判断是否是手机
function is_mobile()
{
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
$is_pc = (strpos($agent, 'windows nt')) ? true : false;
$is_mac = (strpos($agent, 'mac os')) ? true : false;
$is_iphone = (strpos($agent, 'iphone')) ? true : false;
$is_android = (strpos($agent, 'android')) ? true : false;
$is_ipad = (strpos($agent, 'ipad')) ? true : false;
if($is_pc){
return false;
}
if($is_mac){
return true;
}
if($is_iphone){
return true;
}
if($is_android){
return true;
}
if($is_ipad){
return true;
}
}
php如何判断页面是手机端还是pc端PHP 有个手机检测的库很出名啊 Mobile-Detect
地址:
或者
<?php
function isMobile(){
$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
function CheckSubstrs($substrs,$text){
foreach($substrs as $substr){
if(false!==strpos($text,$substr)){
return true;
}else{
return false;
}
}
}
$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','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=CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent);
if ($found_mobile){
return true;
}else{
return false;
}
}
if (isMobile())
echo 'phone';
else
echo 'pc';
?>
php判断客户端是pc还是手机网上现在有三种观点, 一种是基于浏览器发送的 User-Agent, 但明显这种方法是行不通的, 虽然有人列出了大多数的手机发送的User-Agent, 但依然会有很多手机无法识别, 甚至有些手机浏览器不发送User-Agent, 而且也不能保证以后就不会出新牌子. 下面是一个php的例子, 看过之后你就会知道有多么不可靠. 1. function is_wap(){ 2. $ua = strtolower($_SERVER['HTTP_USER_AGENT']); 3. $uachar = "/(nokiasonyericssonmotsamsungsghlgsiephilipspanasonicalcatellenovocldcmidpwapmobile)/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判断pc的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。