douzhouhan4618 2018-08-10 20:08
浏览 37
已采纳

会话与移动设备无法正常工作

Hi,

I have a site with 2 versions, one for the PC and one for the mobile. PC version goes like this mysite.org whereas mobile version goes mysite.org/mobile So when a user visits the site from a mobile device it is automatically redirected to mysite.org/mobile through JS code. That works fine, however, I am offering my users the choice to see the PC version instead so I need a way to tell the browser to stop redirecting once the PC version button is pressed. This is what I did.

<?php
session_start();
$_SESSION['redirect'] = true;

# Check whether the session should be unset.
if ($_GET['no_redirect'] == "true") {
   unset($_SESSION['redirect']);
}

# Check whether the session is set.
if (isset($_SESSION['redirect'])) {
 $redirect = <<<EOF
<script type='text/javascript'>DM_redirect("mobile/$page");</script>
EOF;
}
?>

my html code goes like this

<script type='text/javascript' src='js/mobile.js'></script>
  $redirect

if the $redirect variable is empty, redirection will not occur no matter what device is being used. The variable wont be empty as long as $_SESSION['redirect'] is true. So my button to stop redirecting and see the PC version looks like this:

<a href="&no_redirect=false" rel="alternate">SEE PC VERSION</a>

whereas the button to go back to the mobile version looks like this

<a href="&no_redirect=true" rel="alternate">SEE MOBILE VERSION</a>

it wont quite work because after pressing the PC version button it goes indeed to the PC version page but if I navigate from there it redirects again to the mobile version. What solution can I use?

Thank you.

展开全部

  • 写回答

1条回答 默认 最新

  • dpn4073 2018-08-10 20:39
    关注

    Your problem is that you are always setting $_SESSION['redirect'] to trueand only unsetting it when $_GET['no_redirect'] is set. So your if condition is always true except for when the "PC version" button is pressed. You could try the following code instead:

    session_start();
    // if we haven't set redirect, assume we want to redirect
    if (!isset($_SESSION['redirect'])) $_SESSION['redirect'] = true;
    
    // Do we want to keep redirecting?
    if ($_GET['no_redirect'] == "true") {
        $_SESSION['redirect'] = false;
    }
    
    // Should we redirect?
    if ($_SESSION['redirect']) {
        $redirect = <<<EOF
    <script type='text/javascript'>DM_redirect("mobile/$page");</script>
    EOF;
    }
    

    This code will now only set $_SESSION['redirect'] to true if it wasn't already set. However, that won't let you get back to the mobile version. Assuming that if the "Mobile version" button is pressed, the page will be called with $_GET['no_redirect'] = "false", in which case you can change the first part of the code to this:

    session_start();
    // if we haven't set redirect, assume we want to redirect
    if (!isset($_SESSION['redirect'])) $_SESSION['redirect'] = true;
    
    // Do we want to keep redirecting?
    if ($_GET['no_redirect'] == "true") {
        $_SESSION['redirect'] = false;
    }
    elseif ($_GET['no_redirect'] == "false") {
        $_SESSION['redirect'] = true;
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部