您好,欢迎访问一九零五行业门户网

PHP代码判断设备是手机还是平板电脑(两种方法)

随着互联网移动设备的普及,很多网站都兼容手机端浏览,为了更好的让网页在手机端显示,我们都选择了使用css媒体查询制作响应式模版。本文给大家介绍php代码判断
现在移动互联网越来越发达,,很多的网站都普及了手机端浏览,为了更好的让网页在手机端显示,我们都选择了使用css媒体查询制作响应式模版,但这也有弊端,例如某些网站的结构是cms类型的,太多的内容要显示,而使用css媒体查询设计响应式,只会隐藏但还是加载了,为了让手机端更快速的显示出内容,我们可以使用这个php判断手机设备代码,使用这个代码可以很方便的显示或不显示自定义的内容。
在做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判断手机设备函数代码,复制到php函数库中调用:
这是调用代码,可以加上if判断:
设置手机端的内容
以上所述就是本文的全部内容,希望大家喜欢。
其它类似信息

推荐信息