繁体   English   中英

基于cookie的无限循环php重定向

[英]Infinite loop php redirect based on cookie

我目前正在忙于编写注册页面。 该页面包含三个步骤,每个步骤都有其自己的cookie值。 我想做的是检查Cookie的值,并在访问网站后将用户转移到正确的页面

例如:如果$ _COOKIE ['step']的值为'step_two',则应重定向至:www.domain.com/register.php?step = your_details。 如果未设置cookie,则不应重定向它,并保留在register.php页面上。

重定向工作正常,但陷入无限循环。 我真的已经无法清醒了,因为我已经醒了差不多24小时了。 因此,如果有人能将我推向正确的方向,我将不胜感激。

代码段:

$cookie_value = 'step_2';
setcookie("step",$cookie_value, time()+3600*24);

$cookie_not_set = true;
$cookie_step_two = false;

if (isset($_COOKIE['step'])) {
    if ($_COOKIE['step'] == 'step_2') {
        $cookie_not_set = false;
        $cookie_step_two = true;
        header('Location: ?step=your_details');
        exit();
    }
} else {
    $cookie_not_set = true;
}

谢谢。

您实际上无处设置Cookie值,因此它不会改变。 这就是为什么有无限循环的原因。

$_GET$_COOKIE彼此无关。 看起来像您想要的:

if ($_GET['step'] === 'your_details')`

...这总比使用cookie更好。

您将不断输入您的if条件,因为您的cookie数据没有其他操作。

如果您的Cookie设置为“ step_2”,则您将进入循环。 没有任何更改,请刷新页面。 您将重新输入step_2条件并进入重定向。

我还假设您了解您的$_GET$_COOKIE请求是完全不同的。 如果不是,请参阅@Brads答案


停止此无限循环的解决方案是:

if (isset($_COOKIE['step'])) {
    if ($_COOKIE['step'] == 'step_2') {
        $cookie_not_set = false;
        $cookie_step_two = true;
        $_COOKIE['step'] = 'step_3';
        header('Location: ?step=your_details');
        exit();
    }

但也请注意,您的正确/错误验证/更改是本地更改,在页面刷新时不是绝对的

我相信您的问题是重定向未更改您的cookie,因此,如果将cookie设置为step_2,则需要查看要重新传递的GET var;

$cookie_not_set = true;
$cookie_step_two = false;

if (isset($_COOKIE['step'])) {
    if ($_COOKIE['step'] == 'step_2') {
       if( !empty($_GET['step']) && $_GET['step'] == 'your_details' )
       {
          ... you have redirected and now can continue ...
       }
       else
       {
         // redirect and set the get var to signal to this script.

          $cookie_not_set = false;
          $cookie_step_two = true;
          header('Location: ?step=your_details');
          exit();
        }
    }
} else {
    $cookie_not_set = true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM