【问题标题】:Redirect loop in full site to mobile site using session使用会话将整个站点中的循环重定向到移动站点
【发布时间】:2013-07-31 13:55:05
【问题描述】:

我有一个完整的网站,已经在 OS-commerce 中,移动网站在核心 PHP(codeignitor)中,完整版和子域上的移动版。

例如,完整站点:www.example.com,移动站点域为 m.example.com。当用户在手机中打开全站域名时,网站会重定向正确的移动域名,但如果移动用户想查看全站,则用户可以在移动端查看全站。

我已经使用它来完成重定向http://code.google.com/p/php-mobile-detect/,但它没有重定向到整个站点或使用会话的移动站点。我知道我必须使用 PHP SESSIONS 和 REQUEST 才能让它工作,但我不知道如何在这种情况下使用它们,所以你能建议如何使用会话解决这个重定向问题吗?

我的代码是:

session_start(); 

  include('includes/Mobile_Detect.php');
  $detect = new Mobile_Detect;

 if(isset($_REQUEST['fullsite']) && $_REQUEST['fullsite'] == 'yes')
 {//check if fullsite view request from mobile or website?

    $_SESSION['fullsite']="yes";

    if($detect->isMobile()) {
               $_SESSION['website']="mobile";
    }
    else{
       $_SESSION['website']="computer"; 
    }

    $deviceType = header('Location: https://www.example.com/');
  }
  else
  {
    if($_SESSION['website'] =="mobile"  && $_SESSION['fullsite'] !="yes")
    {
        if($detect->isTablet())
        {
            $deviceType = 'tablet';
        }
        else
        {
            $deviceType = 'phone';
        }

        $deviceType = header('Location: https://m.example.com/');
    }
    elseif($_SESSION['website'] =="computer" && $_SESSION['fullsite'] =="yes")
    {
        $deviceType = 'computer';
        $deviceType = header('Location: https://www.example.com/');
    }
    else{   
        $deviceType = 'computer';
     }

    $scriptVersion = $detect->getScriptVersion();
    session_destroy();
  }

【问题讨论】:

    标签: php codeigniter session oscommerce url-redirection


    【解决方案1】:

    从我可以从 github 页面获得的信息,您应该能够使它像这样工作:

    index.php

    session_start();
    
    if ($_GET['fullscreen'] == 'yes') {
        $_SESSION['fullscreen'] = 1;
    } else if ($_GET['fullscreen'] == 'no') {
        $_SESSION['fullscreen'] = 0;
    }
    
    if (false == isset($_SESSION['fullscreen']) && ($_SESSION['fullscreen'] == 0)) {
        // If session['fullscreen'] has not been set (maybe first visit
        // or the user does not what in fullscree
        // check the device and do redirect
        require_once 'Mobile_Detect.php';
        $detect = new Mobile_Detect();
    
    
        // Any mobile device (phones or tablets).
        if ( $detect->isMobile() ) {
    
        }
        ...
    }
    
    // Other code here
    

    从手机访问时,如果用户想要完整版,请提供一个带有 GET 参数fullscreen=yes (http://example.com?fullscreen=yes) 的 URL 锚点 如果在完整站点上并检测到移动设备(未包含在上面的代码中),您可以使用fullscreen=no提供移动版本的链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多