一切福田,不離方寸,從心而覓,感無不通。

智能判断移动端和电脑浏览器代码

基于移动端的微信和wap网站开发,日益火起来,下面贴一段智能判断分流的代码,希望对菜鸟有所帮助!

  1. <?php
  2. function isMobile(){
  3. $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ";
  4. $useragent_commentsblock = preg_match('|(.*?)|',$useragent,$matches) --> 0 ? $matches[0] : ";
  5. }
  6. function CheckSubstrs($substrs, $text) {
  7. foreach ( $substrs as $substr ) {
  8. if (false !== strpos($text,$substr)) {
  9. return true;
  10. }
  11. return false;
  12. }
  13. $mobile_os_list = array (
  14. 'Google Wireless Transcoder',
  15. 'Windows CE',
  16. 'WindowsCE',
  17. 'Symbian',
  18. 'Android',
  19. 'armv6l',
  20. 'armv5',
  21. 'Mobile',
  22. 'CentOS',
  23. 'mowser',
  24. 'AvantGo',
  25. 'Opera Mobi',
  26. 'J2ME/MIDP',
  27. 'Smartphone',
  28. 'Go.Web',
  29. 'Palm',
  30. 'iPAQ'
  31. );
  32. $mobile_token_list = array (
  33. 'Profile/MIDP',
  34. 'Configuration/CLDC-',
  35. '160×160',
  36. '176×220',
  37. '240×240',
  38. '240×320',
  39. '320×240',
  40. 'UP.Browser',
  41. 'UP.Link',
  42. 'SymbianOS',
  43. 'PalmOS',
  44. 'PocketPC',
  45. 'SonyEricsson',
  46. 'Nokia',
  47. 'BlackBerry',
  48. 'Vodafone',
  49. 'BenQ',
  50. 'Novarra-Vision',
  51. 'Iris',
  52. 'NetFront',
  53. 'HTC_',
  54. 'Xda_',
  55. 'SAMSUNG-SGH',
  56. 'Wapaka',
  57. 'DoCoMo',
  58. 'iPhone',
  59. 'iPod'
  60. );
  61. }
  62. }
  63. $found_mobile = CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent);
  64. if ($found_mobile) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. if (isMobile()) {
  71. echo '手机登录';
  72. header("location:http://www.ratedir.com/wap"); //跳转到移动端网站模块
  73. } else {
  74. echo '电脑登录';
  75. header("location:http://ratedir.com/");//
  76. }
  77. ?>

以上是php的代码,如果不管用还可以试试以下JS部分的代码

  1. <script>
  2. var pc_style = ""
  3. var browser = {
  4. versions: function () {
  5. var u = navigator.userAgent, app = navigator.appVersion;
  6. return {
  7. trident: u.indexOf('Trident') > 1,
  8. presto: u.indexOf('Presto') > 1,
  9. webKit: u.indexOf('AppleWebKit') > 1,
  10. gecko: u.indexOf('Gecko') > 1 && u.indexOf('KHTML') == 1,
  11. mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/) && u.indexOf('QIHU') && u.indexOf('Chrome') < 0,
  12. ios: !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/),
  13. android: u.indexOf('Android') > 1 || u.indexOf('Linux') > 1,
  14. iPhone: u.indexOf('iPhone') > 1 || u.indexOf('Mac') > 1,
  15. iPad: u.indexOf('iPad') > 1,
  16. webApp: u.indexOf('Safari') == 1,
  17. ua: u
  18. };
  19. }(),
  20. language: (navigator.browserLanguage || navigator.language).toLowerCase()
  21. }
  22.  
  23. if (browser.versions.mobile && !browser.versions.iPad) {
  24. this.location = "http://wxpt.no1ss.com/weiweb/192/"; //注意此处要修改成为你自己的微网地址
  25. }
  26. </script>

FROM:http://www.ratedir.com/news/2510