请求新密码后强制用户更改密码


Force user to change password after requesting new password

我有一个项目PHP用户可以登录并在忘记密码时请求新密码。我也有init.php它指导用户在使用新密码登录后更改密码页面。

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只给我user.php,这当然还不够。我也尝试了$_SERVER['REQUEST_URI']这给了我/user.php?p=change_password这很好。但仍然不起作用。我收到错误说The page isn't redirecting properly

所以总结一下。我需要将用户重定向到user.php?p=change_password&force他们是否请求了新密码。

提前致谢

您可能会陷入重定向循环。以下是我认为发生的事情:

  1. 您设法将用户重定向到user.php?p=change_password&force
  2. 进入此页面后,包含 $current_file !== 'user.php?p=change_password' 的 if 语句仍然是 true
  3. 因此,您正在尝试将用户重定向到他/她已经打开的同一页面,从而导致重定向错误
$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();
    }
}

此解决方案有效