如何使用php代码来判断是否是移动设备
现在手机上网越来越普及,很多网站都普及手机浏览。为了更好的在手机端展示网页,我们选择了使用CSS媒体查询来制作响应式Templates,但是这样也有缺点。比如有些网站的结构是CMS类型的,显示的内容太多,而用CSS媒体查询设计响应式,只会隐藏但仍然加载,为了让手机端显示更快要显示内容,我们可以使用这个PHP判断手机设备的代码。使用此代码可以轻松显示或不显示自定义内容。
这是PHP判断手机设备函数代码,复制到PHP函数库中调用:<?phpfunction is_mobile() {$user_agent = $_SERVER['HTTP_USER_AGENT'];$mobile_browser = Array("mqqbrowser", //手机QQ浏览器"opera mobi", //手机opera"juc","iuc",//uc浏览器"fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod","iemobile", "windows ce",//windows phone"240×320","480×640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte");$is_mobile = false;foreach ($mobile_browser as $device) {if (stristr($user_agent, $device)) {$is_mobile = true;break;}}return $is_mobile;}?>这是调用代码,可以加上if判断:<?php if(is_mobile()):?>设置手机端的内容<?php endif; ?>这是PHP判断手机设备函数代码,复制到PHP函数库中调用: <?php function is_mobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_browser = Array( "mqqbrowser", //手机QQ浏览器 "opera mobi", //手机opera "juc","iuc",//uc浏览器 "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod", "iemobile", "windows ce",//windows phone "240×320","480×640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte" ); $is_mobile = false; foreach ($mobile_browser as $device) { if (stristr($user_agent, $device)) { $is_mobile = true; break; } } return $is_mobile; }?> 这是调用代码,可以加上if判断: <?php if(is_mobile()):?> 设置手机端的内容 <?php endif; ?>这是PHP判断手机设备函数代码,复制到PHP函数库中调用: <?php function is_mobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_browser = Array( "mqqbrowser", //手机QQ浏览器 "opera mobi", //手机opera "juc","iuc",//uc浏览器 "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod", "iemobile", "windows ce",//windows phone "240×320","480×640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte" ); $is_mobile = false; foreach ($mobile_browser as $device) { if (stristr($user_agent, $device)) { $is_mobile = true; break; } } return $is_mobile; }?> 这是调用代码,可以加上if判断: <?php if(is_mobile()):?> 设置手机端的内容 <?php endif; ?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容