关注前端开发
关注用户体验

php判断用户是否使用手机设备

搞移动页面调整的时候,需要用到这个东西,服务器端来判断用户的设备是不是手机。之前的d4在手机设备上浏览的时候,侧边栏是display:none的,这点很不人性化。手持设备注重的是速度和体验,没必要的东西最好不要上去,上去了又给隐藏掉,有点二。所以后来想想,如果在后端用php判断用户是否使用的是手机,那句比较轻松的防止加载没必要的东西了。之前是用css3来判断屏幕宽度,然后重定义css样式以获得最佳浏览体验。这个方法看似可行,其实病处不少,这个待后来慢慢讨论吧。回到本文的主题,php判断用户是否使用手机。

第一步:如果你和我一样使用的是wordpress,那就复制到主题的 function.php 文件

//判断是否属手机
function is_mobile() {
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	$mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
	$is_mobile = false;
	foreach ($mobile_agents as $device) {
		if (stristr($user_agent, $device)) {
			$is_mobile = true;
			break;
		}
	}
	return $is_mobile;
}

第二步:if 语句

if( is_mobile() ){
Your Code
}
注意:是php

ok,看看d4主题使用的这种办法:

未经允许不得转载:大前端 » php判断用户是否使用手机设备
分享到:更多 ()

评论 15

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #12

    uc都判断不了。。。。这是wp8上uc的agent。。。mozilla/5.0 (compatible; msie 10.0; windows nt 6.2; trident/6.0; arm; touch; wpdesktop) ucbrowser/3.5.0.410
    难道只能判断wap?

    彩云贝3年前 (2014-05-16)回复
    • 对一些这三年来的新浏览器可能支持确实不太好。。吧。。 ➡

      Esec3年前 (2014-07-03)回复
  2. #11

    已经使用了

    撒哈拉的小猫3年前 (2014-04-19)回复
  3. #10

    功能很不错哦 谢谢博主

    爱就是幸福4年前 (2013-08-12)回复
  4. #9

    :mrgreen:

    voips5年前 (2012-11-29)回复
  5. #8

    如果能分离出来,写个php通用的判断语句就好了。。

    ichendong5年前 (2012-10-18)回复
  6. #7

    感谢分享的好文章

    长白论坛6年前 (2011-08-25)回复
  7. #6

    你就不能用in_array()函数?

    李大朋6年前 (2011-08-12)回复
  8. #5

    Hell yes, i absence write something taste this however didnt require period, may i repost this php判断用户是否使用手机设备 – 大前端

    Victoria6年前 (2011-08-01)回复
  9. #4

    再做个简版手机主题,手机访问的时候就跳过去 😳

    palese6年前 (2011-08-01)回复
  10. #3

    手机版也超酷 哈哈哈哈

    我行我骕6年前 (2011-07-31)回复
  11. #2

    做个手机页面还是不错的,即使自己用也好,方便随时查看回复留言 😳

    Sogei6年前 (2011-07-31)回复
    • 是的,现在是移动互联时代,需要适应

      浩子6年前 (2011-08-01)回复
  12. #1

    记得以前使用Emlog3.x版的时候使用过相似的语句,现在都不去搞这些了,因为除了我自己还真没什么人用手机来访问 ➡

    那些事儿6年前 (2011-07-29)回复

themebetter 国内更好的WordPress主题服务商

立即前往