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

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主题使用的这种办法:

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

评论 33

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

    I like the helpful information you provide in your articles. I will bookmark your blog and check again here regularly. I am quite certain I will learn a lot of new stuff right here! Best of luck for the next!

  2. #29

    By far the most effective option to see what Uncode can do for you is to visit their web site and
    browse the showcase of person-created websites.
    In case you are planning to make a web site by yourself, listed here are some helpful suggestions that you need to use
    to get pleasure from the absolute best results.
    I know, it’s not that spectacular, but let’s
    make it an actual site with real information.
    Posting free articles to the main article directories websites is
    the fastest and best free solution to make more sales, improve your google rating, boost immediate site visitors
    getting backlinks, hyperlink constructing and push your alexa rating via the roof.
    Many webmasters like the thought of adding background music to their websites however
    most shrink back from doing it worrying about sluggish loading pages and enormous
    file sizes. Although some smartphones can display
    sites utilizing actual internet browsers
    they do not have switch speeds as quick as computer systems do.

    Jasa Website3周前 (06-06)回复
  3. #28

    very interesting subject , appreciate it for putting up.

    Jayson Poette1个月前 (05-15)回复
  4. #27

    Great – I should definitely pronounce, impressed with your website. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or something, web site theme . a tones way for your client to communicate. Nice task.

  5. #26

    I enjoy reading through an article that can make people think. Also, many thanks for allowing me to comment!

    DMC52个月前 (05-11)回复
  6. #25

    Hi! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no back up. Do you have any solutions to protect against hackers?

    Nonton Anime2个月前 (05-10)回复
  7. #24

    Thanks for sharing excellent information. Your web site is so cool. I am impressed by the details that you have on this blog. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles.

    cat care2个月前 (05-08)回复
  8. #23

    Hello there! This article couldn’t be written any better! Going through this article reminds me of my previous roommate! He continually kept preaching about this. I am going to forward this article to him. Pretty sure he’ll have a very good read. Thank you for sharing!

    DMC52个月前 (05-07)回复
  9. #22

    Hi, Neat post. There is an issue together with your website in web explorer, may test this… IE nonetheless is the market chief and a good portion of people will miss your excellent writing due to this problem.

    bisnis online2个月前 (04-22)回复
  10. #21

    I really like your writing style, excellent information, thankyou for putting up : D.

  11. #20

    Hello, you used to write wonderful, but the last several posts have been kinda boring… I miss your super writings. Past several posts are just a little bit out of track! come on!

  12. #19

    I’m not sure where you’re getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic info I was looking for this info for my mission.

    sewa mobil2个月前 (04-19)回复
  13. #18

    Enjoyed the post.

    schoonheidsspecialist2个月前 (04-19)回复
  14. #17

    I just added this web site to my rss reader, excellent stuff. Can’t get enough!

    Chelsey Lyerla3个月前 (04-08)回复
  15. #16

    Unquestionably believe that which you stated. Your favorite justification appeared to be on the net the simplest thing to be aware of. I say to you, I definitely get irked while people think about worries that they just don’t know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people can take a signal. Will likely be back to get more. Thanks

    pengeluaran hk3个月前 (04-05)回复
  16. #15

    Pretty section of content. I just stumbled upon your web
    site and in accession capital to assert that I acquire actually
    enjoyed account your blog posts. Any way I will be subscribing to your
    augment and even I achievement you access consistently
    quickly.

  17. #14

    Wow! This could be one of the most beneficial blogs we have ever come across on thesubject. Actually wonderful post! I am also a specialist in this topic so I can understand your effort.

    my singing monsters3个月前 (03-28)回复
  18. #13

    The the next occasion I just read a blog, I really hope which it doesnt disappoint me approximately this one. I mean, Yes, it was my choice to read, but I actually thought youd have some thing interesting to state. All I hear is actually a number of whining about something you could fix if you werent too busy in search of attention.

    Gino Microni4个月前 (03-11)回复
  19. #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?

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

      Esec5年前 (2014-07-03)回复
  20. #11

    已经使用了

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

    功能很不错哦 谢谢博主

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

    :mrgreen:

    voips7年前 (2012-11-29)回复
  23. #8

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

    ichendong7年前 (2012-10-18)回复
  24. #7

    感谢分享的好文章

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

    你就不能用in_array()函数?

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

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

    Victoria8年前 (2011-08-01)回复
  27. #4

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

    palese8年前 (2011-08-01)回复
  28. #3

    手机版也超酷 哈哈哈哈

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

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

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

      浩子8年前 (2011-08-01)回复
  30. #1

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

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

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

立即前往