简体   繁体   English

要求用户在输入新密码后更改密码

[英]Force user to change password after requesting new password

I have PHP project where users can login and request new password if they forget. 我有一个PHP项目,用户可以在忘记密码的情况下登录并请求新密码。 I also have init.php where it directs the user to change password page after logging in with new password. 我还拥有init.php ,它在此引导用户使用新密码登录后更改密码页面。

The code in INIT.PHP to redirect the user is as follows: INIT.PHP用于重定向用户的代码如下:

$current_file = explode('/', $_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);

if (logged_in() === true) {

    if (user_active($user_data['username']) === false) {
        session_destroy();
        header('Location: index.php');
        exit();
    }

    if ($current_file !== 'user.php?p=change_password' && $user_data['password_recover'] == 1) {
        header('Location: user.php?p=change_password&force');
        exit();
    }

}

$current_file is giving me user.php only and this is not enough of course. $current_file仅给我user.php ,这当然是不够的。 I have also tried $_SERVER['REQUEST_URI'] which gives me /user.php?p=change_password which is fine. 我还尝试了$_SERVER['REQUEST_URI'] ,它给了我/user.php?p=change_password很好。 But still does not work. 但是仍然不起作用。 I get errors saying The page isn't redirecting properly 我收到错误消息,指出The page isn't redirecting properly

So to sum up. 综上所述。 I need to redirect the user to user.php?p=change_password&force if they have requested a new password. 如果他们已请求新密码,则需要将用户重定向到user.php?p=change_password&force

Thanks in advance 提前致谢

You might be stuck in a redirect loop. 您可能会陷入重定向循环。 Here's what I think happens: 我认为会发生以下情况:

  1. You manage to redirect the user to user.php?p=change_password&force 您设法将用户重定向到user.php?p=change_password&force
  2. Once you're on this page, the if statement containing $current_file !== 'user.php?p=change_password' is still true 进入此页面后,包含$current_file !== 'user.php?p=change_password'的if语句仍然为true
  3. Hence, you're trying to redirect the user to the same page that s/he is already on, causing a redirection error 因此,您试图将用户重定向到他/他已经在的同一页面上,从而导致重定向错误
$current_url = explode('/', $_SERVER['REQUEST_URI']);
$current_url = end($current_url);

if (logged_in() === true) {

    if (user_active($user_data['username']) === false) {
        session_destroy();
        header('Location: index.php');
        exit();
    }

    if ($current_url !== 'user.php?p=change_password' && $user_data['password_recover'] == 1) {
        header('Location: user.php?p=change_password');
        exit();
    }

}

This solution worked 此解决方案有效

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

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