Index.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. class Index extends Frontend
  5. {
  6. protected $noNeedLogin = '*';
  7. protected $noNeedRight = '*';
  8. protected $layout = '';
  9. public function index()
  10. {
  11. $isMobile = $this->isMobile();
  12. if ($isMobile) {
  13. //echo "手机访问";
  14. $this->redirect("/h5"); //关键代码重定向
  15. } else {
  16. //echo "PC访问";
  17. $this->redirect("/dist"); //关键代码重定向
  18. }
  19. // return $this->view->fetch();
  20. }
  21. function isMobile() {
  22. // 获取用户代理字符串
  23. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  24. // 判断是否为手机
  25. $mobile_agents = array('iphone', 'ipod', 'ipad', 'android', 'mobile', 'blackberry', 'webos', 'incognito', 'webmate', 'bada', 'nokia', 'lg', 'ucweb', 'skyfire');
  26. $is_mobile = false;
  27. foreach ($mobile_agents as $device) {
  28. if (strstr($user_agent, $device)) {
  29. $is_mobile = true;
  30. break;
  31. }
  32. }
  33. return $is_mobile;
  34. }
  35. }