标头位置问题.如果其他语句似乎找不到问题


Header location issue. Cant seem to find the issue in if else statement

我对以下PHP代码有一些问题:

require '../core/connection.php';
require '../includes/functions.php';
if (isset($_SESSION['user_id'])) {
    if (isset($_POST['current_user_password'])) {
        $current_user_password = md5($_POST['current_user_password']);
        if ($current_user_password != $_SESSION['user_password']) {
            header('Location:../edit_credentials.php?edit=password&error=current_password');
        } else {
            $new_password = $_POST['new_user_password'];
            $new_password2 = $_POST['new_user_password2'];
            if ($new_password == '' || ($new_password2 == '') {
                header('Location:../edit_credentials.php?edit=password&error=new_password_empty');
            }
            if ($new_password != $new_password2) {
                header('Location:../edit_credentials.php?edit=password&error=new_password_not_equal');
            } else {
                $new_password = md5($new_password);
                edit_user_password($user_id,$new_password);
            }
        }
    } else {
        echo 'current_user_password not set';
    }
} else {
    echo 'Session not set';
}

这也是函数:

function edit_user_password($user_id,$user_password){
    global $db;
    $user_password_data = $db->query('
        UPDATE `users`
        SET `user_password` = "'.$user_password.'" WHERE `user_id` = "'.$user_id.'";');
        $_SESSION['user_password'] = $user_password;
        $db->query($user_password_data);
        header("Location:../edit_credentials.php?saved=password");
}

除了这部分之外,一切都有效:

if ($new_password == '' || ($new_password2 == '') {
                header('Location:../edit_credentials.php?edit=password&error=new_password_empty');
            }

当涉及到密码为空的这一部分时,如果我使用 die('test') 切换重定向,它可以工作,但它不适用于重定向。你们有机会知道为什么这不起作用吗?

提前感谢所有帮助。

您应该在每次header('Location:...');调用后添加一个exit;。否则,脚本将继续运行。