php 如何在php中检查请求是来自手机还是电脑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5335237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if the request came from mobile or computer in php
提问by Khurram Ijaz
i need to check if the request came from a mobile phone device or desktop computer using php please help. thanks
我需要检查请求是否来自移动电话设备或使用 php 的台式计算机,请帮忙。谢谢
回答by Shameer
I am using a function to identify mobile browsers in my projects, which can detect almost all major Mobile Operating systems and browsers.
我正在使用一个函数来识别我的项目中的移动浏览器,它可以检测几乎所有主要的移动操作系统和浏览器。
function ismobile() {
$is_mobile = '0';
if(preg_match('/(android|up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$is_mobile=1;
}
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$is_mobile=1;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array('w3c ','acs-','alav','alca','amoi','andr','audi','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','oper','palm','pana','pant','phil','play','port','prox','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
if(in_array($mobile_ua,$mobile_agents)) {
$is_mobile=1;
}
if (isset($_SERVER['ALL_HTTP'])) {
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
$is_mobile=1;
}
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
$is_mobile=0;
}
return $is_mobile;
}
回答by Mike Lewis
Check the $_SERVER['HTTP_USER_AGENT']
for mobile user agents.
检查$_SERVER['HTTP_USER_AGENT']
移动用户代理。
You should check out http://detectmobilebrowser.com/for already existing scripts to detect for mobile browsers (it just uses the user agents).
您应该查看http://detectmobilebrowser.com/以获取现有脚本以检测移动浏览器(它仅使用用户代理)。
回答by user501869
You can try The WURFL PHP APIor Tera-Wurfl
回答by Leon
http://mobiledetect.net/is another lightweight php class, but you mist keep in mind that checking User-agent is a good but not perfect way, the issue is rules are constantly out-dated and incomplete so you need to change the detection code continuously. Also check https://modernizr.com/for another way to detect.
http://mobiledetect.net/是另一个轻量级的 php 类,但请记住,检查 User-agent 是一种很好但并不完美的方法,问题是规则经常过时且不完整,因此您需要更改检测码不断。另请检查https://modernizr.com/以获取另一种检测方式。
回答by jotaelesalinas
Nowadays, what do you consider "a mobile device"?
如今,您认为什么是“移动设备”?
User agent parsers will give very good results, except for rare edge cases. The problem is that you have to constantly update them if you store the data locally or depend on the service being online if you use it "in the cloud".
用户代理解析器将给出非常好的结果,除了罕见的边缘情况。问题是,如果您将数据存储在本地或依赖在线服务(如果您在“云中”使用它),则您必须不断更新它们。
It is better to use a functionality detection library, e.g. Modernizr, send to your server information about the browser capabilities on first visit and serve appropriate content based on what the browser can or cannot do. Or even better, delegate that to Javascript.
最好使用功能检测库,例如Modernizr,在首次访问时将有关浏览器功能的信息发送到您的服务器,并根据浏览器可以做什么或不能做什么来提供适当的内容。或者更好的是,将其委托给 Javascript。
回答by Doc-Han
This is the simplest way to do it in PHP.
这是在 PHP 中执行此操作的最简单方法。
if(stristr($_SERVER['HTTP_USER_AGENT'],'Mobile')){
//Put code here when device is mobile
}else{
//Put code here when device is not a mobile
}