duanji2772 2018-03-14 05:32
浏览 26
已采纳

通过URL设置和取消设置会话

Hi,

I need to create a session as soon as the visitor enters my page. Then by clicking on a link that takes to an URL like this example.org/page?no_redirect=true the session must be destroyed but the session should be created again if they click on a link to this URL example.org/page?no_redirect=false.

I did it like this:

session_start();
$_SESSION['redirect'] = "false";
if($_GET['no_redirect'] == "true")
{
 $_SESSION['redirect']="true";
} elseif ($_GET['no_redirect'] == "false") {
 $_SESSION['redirect']="false";
}

if ($_SESSION['redirect']!=true) {
$redirect = <<<EOF
<script type='text/javascript'>DM_redirect("mobile/$page");</script>
EOF;
}

but its not working. What could it be?

Thank you.

  • 写回答

1条回答 默认 最新

  • doujia7517 2018-03-14 05:50
    关注

    The check if ($_SESSION['redirect'] != true) makes no sense, because you are comparing a non-empty string to a boolean. Non-empty strings always evaluate to true, so your check is really if (true != true), which means the content inside the block will never be executed.

    A more sensible approach would be to unset your session once its purpose has been served instead of setting it to "true" / "false".

    Code:

    session_start();
    
    # Check whether the session should be unset.
    if ($_GET['no_redirect'] == "true") {
       unset($_SESSION['redirect']);
    }
    
    # Check whether the session should be set.
    else if ($_GET['no_redirect'] == "false") {
       $_SESSION['redirect'] = "true";
    }
    
    # Check whether the session is set.
    if (isset($_SESSION['redirect'])) {
       $redirect = <<<EOF
       <script type='text/javascript'>DM_redirect("mobile/$page");</script>
       EOF;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题