码迷,mamicode.com
首页 > 移动开发 > 详细

php获得访问来源(手机wap访问、电脑web访问)

时间:2015-11-04 17:34:16      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

为了更好的用户体验,本主题默认支持wap和web两种访问样式,wap访问将不包含任何脚本、样式也是重新撰写,不支持更多页面,只支持首页、详情页, 评论框、登录等内容将陆续更新到wap内容里。如果想测试,您可以使用手机访问http://qianduanblog.com试试看,是不是效果和电脑 访问不一样?

下面贴出获得访问类型的函数(来源于互联网):

经测试,下面判断是错误的。

  1. /***************************************************
  2. ** 返回访问的类型(wap web)
  3. ** 2012年12月2日22:21:20
  4. ***************************************************/
  5. function yundanran_get_access_type()
  6. {
  7. $is_wap=false;
  8. // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  9. if (isset($_SERVER[‘HTTP_X_WAP_PROFILE‘]))
  10. {
  11. $is_wap=true;
  12. }
  13. else
  14. {
  15. // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  16. if(isset($_SERVER[‘HTTP_VIA‘]))
  17. {
  18. // 找不到为flase,否则为true
  19. $is_wap=stristr($_SERVER[‘HTTP_VIA‘],"wap") ? true : false;
  20. }
  21. else
  22. {
  23. // 协议法,因为有可能不准确,放到最后判断
  24. if (isset($_SERVER[‘HTTP_ACCEPT‘]))
  25. {
  26. // 如果只支持wml并且不支持html那一定是移动设备
  27. // 如果支持wml和html但是wml在html之前则是移动设备
  28. if
  29. (
  30. (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) !== false)
  31. && (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘) === false
  32. ¦¦ (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) < strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘)))
  33. )
  34. {
  35. $is_wap=true;
  36. }
  37. }
  38. }
  39. }
  40. return $is_wap?‘wap‘:‘web‘;
  41. }
  42. 正确的判断方法,将会近期写出:

    ╮(╯▽╰)╭,笨方法,用UA判断,源自大前端:

  43.  

    1. /***************************************************
    2. ** 返回访问的类型(wap web)
    3. ** 2012年12月2日22:21:20
    4. ** 2012年12月3日22:30:00
    5. ***************************************************/
    6. function yundanran_get_access_type()
    7. {
    8. $is_wap=false;
    9. $user_agent = $_SERVER[‘HTTP_USER_AGENT‘];
    10. $mobile_agents = Array
    11. (
    12. "240x320","acer","acoon","acs-","abacho","ahong","airness",
    13. "alcatel","amoi","android","anywhereyougo.com","applewebkit/525",
    14. "applewebkit/532","asus","audio","au-mic","avantogo",
    15. "becker","benq","bilbo","bird","blackberry","blazer","bleu",
    16. "cdm-","compal","coolpad","danger","dbtel","dopod","elaine",
    17. "eric","etouch","fly ","fly_","fly-","go.web","goodaccess",
    18. "gradiente","grundig","haier","hedy","hitachi","htc",
    19. "huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser",
    20. "kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5",
    21. "lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo",
    22. "mercator","meridian","micromax","midp","mini","mitsu",
    23. "mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen",
    24. "nexian","nf-browser","nintendo","nitro","nokia","nook",
    25. "novarra","obigo","palm","panasonic","pantech","philips",
    26. "phone","pg-","playstation","pocket","pt-","qc-","qtek",
    27. "rover","sagem","sama","samu","sanyo","samsung","sch-",
    28. "scooter","sec-","sendo","sgh-","sharp","siemens","sie-",
    29. "softbank","sony","spice","sprint","spv","symbian","tablet",
    30. "talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba",
    31. "tsm","up.browser","utec","utstar","verykool","virgin","vk-",
    32. "voda","voxtel","vx","wap","wellco","wig browser","wii",
    33. "windows ce","wireless","xda","xde","zte"
    34. );
    35. $is_wap = false;
    36. foreach ($mobile_agents as $device)
    37. {
    38. if (stristr($user_agent, $device))
    39. {
    40. $is_wap = true;
    41. break;
    42. }
    43. }
    44. return $is_wap?‘wap‘:‘web‘;
    45. }
    46. 转载自 http://qianduanblog.com/post/php-get-access-source.html

php获得访问来源(手机wap访问、电脑web访问)

标签:

原文地址:http://www.cnblogs.com/zopgxf/p/4936478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!