【问题标题】:Redirect website to mobile site and from mobile to desktop?将网站重定向到移动网站并从移动设备重定向到桌面?
【发布时间】:2014-03-07 22:36:39
【问题描述】:

如果用户希望从移动设备访问桌面站点,我该如何处理?

我正在使用这个库重定向到移动设备,以及如何创建重定向。下面的代码是我正在使用 mobile_detect.php 库:

require_once 'controller/Mobile_Detect.php';
$detect = new Mobile_Detect;

// Any mobile device (phones or tablets).
if ( !$detect->isMobile() ) {

   //the link to mobile site

}

但是反之如何处理呢?

感谢您的帮助

【问题讨论】:

  • 不要触发重定向脚本,如果用户选择了桌面版本,也许你可以在会话中存储一个值
  • 你能给我看看吗?
  • 添加了一个示例答案

标签: php redirect mobile desktop


【解决方案1】:
<a href="self.php?desktop=1">Switch to desktop</a>

<?php
if(!isset($_SESSION['desktop'])) {
    $_SESSION['desktop'] = false;
}

if(isset($_GET['desktop']) && $_GET['desktop'] == 1) {
    $_SESSION['desktop'] = true;
}

if(!$_SESSION['desktop']) {
    if ( !$detect->isMobile() ) {

       //the link to mobile site

    }

}

在您的移动网站上,您可以通过传递desktop=1 的get 参数让人们有机会切换到桌面(完成重定向的页面)。通常情况下,如果没有传递参数,并且没有设置会话,则为 false。在 $_SESSION['desktop'] 的错误值上,您继续您的脚本,以便它重定向到移动设备。但是一旦传递了参数,它会将会话更改为true,并且您的阻止将不会被执行,因此该站点的正常(桌面)内容将可见

【讨论】:

    【解决方案2】:

    我用这个

    将其放入您的主站点(非移动)

        @include("Mobile_Detect.php");
    
    $detect = new Mobile_Detect();
    
    if ($detect->isMobile() && isset($_COOKIE['mobile']))
    
    {
    
    $detect = "false";
    
    }
    
    elseif ($detect->isMobile())
    
    {
    header("Location:http://whaterver.com");
    }
    

    然后在你的移动应用页面放

    <?php setcookie("mobile","m", time()+3600, "/",".whatever.com"); ?>
    

    然后您可以使用常规链接链接回完整站点,因为“完整站点”将检查 cookie,如果设置了则不会重定向。这将是因为他们的移动应用页面已设置它。

    【讨论】:

    • 搞清楚 IMO 这么简单的事情
    • 如果我想从移动站点转到完整站点,并且在移动站点上有完整站点的链接,该怎么办?
    • 你是老板。谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多