简体   繁体   English

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

[英]Infinite loop php redirect based on cookie

I'm currenting busy coding a registration page. 我目前正在忙于编写注册页面。 The page has three steps and every step has its own cookie value. 该页面包含三个步骤,每个步骤都有其自己的cookie值。 What I'd like to do is checking for the cookies value and transfer the user to the correct page upon visiting the website 我想做的是检查Cookie的值,并在访问网站后将用户转移到正确的页面

Example: if the value of $_COOKIE['step'] is 'step_two' it should redirect to: www.domain.com/register.php?step=your_details. 例如:如果$ _COOKIE ['step']的值为'step_two',则应重定向至:www.domain.com/register.php?step = your_details。 If the cookie's not set, it should not redirect and stay on the register.php page. 如果未设置cookie,则不应重定向它,并保留在register.php页面上。

The redirecting is working 'fine', but it gets into an infinite loop. 重定向工作正常,但陷入无限循环。 I really cant think clear anymore as I've been awake for almost 24h now. 我真的已经无法清醒了,因为我已经醒了差不多24小时了。 Therefor I would appreciate it if anyone could push me into the right directions. 因此,如果有人能将我推向正确的方向,我将不胜感激。

Piece of code: 代码段:

$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;
}

Thank you. 谢谢。

Nowhere are you actually setting your cookie value, so it won't change. 您实际上无处设置Cookie值,因此它不会改变。 That's why you have an infinite loop. 这就是为什么有无限循环的原因。

$_GET and $_COOKIE have nothing to do with each other. $_GET$_COOKIE彼此无关。 It looks like you want: 看起来像您想要的:

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

...which would be better than using a cookie anyway. ...这总比使用cookie更好。

You are going to constantly enter your if condition as there is no other manipulations going on to your cookie data. 您将不断输入您的if条件,因为您的cookie数据没有其他操作。

if your cookie is set to "step_2" you will enter the loop. 如果您的Cookie设置为“ step_2”,则您将进入循环。 No changes are in place, so on the refresh to the page. 没有任何更改,请刷新页面。 You will re-enter the step_2 condition and be into a redirect. 您将重新输入step_2条件并进入重定向。

I'm also assuming that you understand that your $_GET & $_COOKIE requests are completely different. 我还假设您了解您的$_GET$_COOKIE请求是完全不同的。 If not, see @Brads answer 如果不是,请参阅@Brads答案


A solution to stop this infinite loop would be: 停止此无限循环的解决方案是:

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();
    }

But also take note, your true/false validations/changes are local changes and will not be absolute on page refresh 但也请注意,您的正确/错误验证/更改是本地更改,在页面刷新时不是绝对的

I believe your issue is the redirect is not changing your cookie, so you need to look at the GET var you a re passing if the cookie is set to step_2 thus; 我相信您的问题是重定向未更改您的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