在做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