设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 203|回复: 1

Auto-detecting PC/Mobile and then Redirecting

[复制链接]
发表于 2014-3-2 14:15:05 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2014-3-3 06:19 编辑

  • Create two folders in your server called "mobile" and "pc", each containing the respective versions of your site.
  • Copy the code below.
  • Replace the red links with your Mobile and PC destinations.
  • Save and upload as your index.php
Code:

  1. <?php

  2. $mobile_browser = '0';

  3. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
  4.     strtolower($_SERVER['HTTP_USER_AGENT']))){
  5.     $mobile_browser++;
  6.     }

  7. if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
  8.     ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))){
  9.     $mobile_browser++;
  10.     }

  11. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  12. $mobile_agents = array(
  13.     'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  14.     'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  15.     'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  16.     'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  17.     'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  18.     'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  19.     'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  20.     'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  21.     'wapr','webc','winw','winw','xda','xda-');

  22. if(in_array($mobile_ua,$mobile_agents)){
  23.     $mobile_browser++;
  24.     }
  25. if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
  26.     $mobile_browser++;
  27.     }
  28. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
  29.     $mobile_browser=0;
  30.     }


  31. if($mobile_browser>0){
  32.    header('Location: <font color="Red"><b>http://YourSite.mobi/mobile</b></font>');
  33.    } else {
  34.    header('Location: <font color="Red"><b>http://YourSite.mobi/pc</b></font>');
  35.    }
  36.    
  37. ?>
复制代码
More info.

ALTERNATIVES:



I've created a WURFL zipped file that contains everything you need, and makes it incredibly easy, for your .mobi site to detect whether the visitor is on a PC or a Mobile and to redirect accordingly.

Simply download the WURFL.zip file below, extract the files, open readmefirst.txt, and follow the 5 simple steps. Couldn't be easier!


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 发表于 2014-3-2 14:27:53 | 显示全部楼层
  1. <?php

  2. $mobile_browser = '0';

  3. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
  4. $mobile_browser++;
  5. }

  6. if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
  7. $mobile_browser++;
  8. }

  9. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  10. $mobile_agents = array(
  11. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  12. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  13. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  14. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  15. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  16. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  17. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  18. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  19. 'wapr','webc','winw','winw','xda','xda-');

  20. if(in_array($mobile_ua,$mobile_agents)) {
  21. $mobile_browser++;
  22. }

  23. if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
  24. $mobile_browser++;
  25. }

  26. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
  27. $mobile_browser=0;
  28. }

  29. else {
  30. if($mobile_browser>0) {
  31.     header("Location:  YOURWEBSITEHERE"); /* Redirect browser */
  32.     exit;  /* Make sure that code below does not get executed after redirect. */
  33. }
  34. }

  35. ?>
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-27 23:09 , Processed in 0.020309 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表