您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一种 PHP 判断设备是否是手机/平板的代码

2024/2/18 15:33:47发布150次查看
在做web开发的时候经常会需要用到对移动设备的页面匹配,当然可以直接把网站做成响应式的,但如果不想这么做的话,可以使用php对设备类型进行判断, 然后显示相应的界面和内容。今天分享一种使用 php 判断设备是否是手机/平板的方法,方法来源于wordpress(wp-includes/vars.php:125),适用于大部分类型的手机/平板判 断: /** * test if the current browser runs on a mobile device (smart phone, tablet, etc.) * * @staticvar bool $is_mobile * * @return bool */function wp_is_mobile() { static $is_mobile = null; if ( isset( $is_mobile ) ) { return $is_mobile; } if ( empty($_server['http_user_agent']) ) { $is_mobile = false; } elseif ( strpos($_server['http_user_agent'], 'mobile') !== false // many mobile devices (all iphone, ipad, etc.) || 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 ) { $is_mobile = true; } else { $is_mobile = false; } return $is_mobile;}
复制代码
php
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product