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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch