爱站屋博客 - 好东西不私藏,大家一起分享!关于博客 留言板

网站首页 技术教程 正文

PHP代码判断手机移动设备

星云 2018-12-05 技术教程 399 ℃
[db:标签]动判断函数is_mobile(),这个函数在module.php文件中添加,我们可以对其修改一下,让它涵盖更多的移动设备检测功能!
本文转载WRZ,最近没怎么更新,先凑合一下。为了证明我是一个勤快的人...

方法一:is_mobile()原函数

  1. function is_mobile() {
  2. static $is_mobile;
  3. if ( isset($is_mobile) )
  4. return $is_mobile;
  5. if ( emptyempty($_SERVER['HTTP_USER_AGENT']) ) {
  6. $is_mobile = false;
  7. } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
  8. || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
  9. || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
  10. || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
  11. || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
  12. || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
  13. || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
  14. $is_mobile = true;
  15. } else {
  16. $is_mobile = false;
  17. }
  18. return $is_mobile;
  19. }

方法二:is_mobile()原函数

  1. function is_mobile() {
  2. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  3. $mobile_browser = Array(
  4. "mQQbrowser", //手机QQ浏览器
  5. "opera mobi", //手机opera
  6. "juc","iuc",//uc浏览器
  7. "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",
  8. "iemobile", "windows ce",//windows phone
  9. "240x320","480x640","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"
  10. );
  11. $is_mobile = false;
  12. foreach ($mobile_browser as $device) {
  13. if (stristr($user_agent, $device)) {
  14. $is_mobile = true;
  15. break;
  16. }
  17. }
  18. return $is_mobile;
  19. }

函数的调用方法也很简单:

  1. <?php if(is_mobile()): ?>
  2. 你的
  3. <?php else: ?>
  4. 你的非移动设备内容
  5. <?php endif; ?>

猜你喜欢

本文暂时没有评论哦(●'◡'●)

欢迎 发表评论:

:如果评论未出现,请刷新网页,谢谢合作!

会员中心
搜索
ip签名图
    IP签名图
网站分类
标签列表
文章归档
站点信息
  • 文章总数:43994
  • 页面总数:3
  • 分类总数:12
  • 标签总数:629
  • 评论总数:1172
  • 浏览总数:457691
爱站云安全认证