0

我正在创建一个基本的登录和注册页面,并在完成更改密码表单后,我想重定向到 changepassword.php?success。如果进入浏览器,重定向页面可以正常工作,但是在提交表单时,它会重新加载 changepassword.php 页面而不是 ?success 并且从 php 代码块向下的所有内容都不会显示(即表单、右侧列和页脚)。下面是我的 changepassword.php 代码:

<!DOCTYPE html>
<html>
<?php 
include ("storescripts/init.php");
protect_page();
include ("includes/overall/head.php");

if (empty($_POST) ===  false){
$required_fields = array('current_password','password','password_again');
foreach ($_POST as $key=>$value) {
    if (empty($value) && in_array($key, $required_fields) == true) {
        $errors[] = 'Fields marked with an asterisk are required';
        break 1;
    }
}
if ($_POST['current_password'] === $member_data['mem_password']) {
    if(trim($_POST['password']) !== trim($_POST['password_again'])){
        $errors[] = 'Your new passwords do not match';
    } else if (strlen($_POST['password']) <6) {
        $errors[] = 'Your password must be at least 6 characters';
    }
} else {
    $errors[] = 'Your current password is incorrect';
}
}
?>
<body>
<?php include ("includes/overall/template_header.php");?>
<div id="mainDivShort">
    <h1>Change Password</h1>
    <div id="divBreak"></div>
    <?php include ("includes/overall/column_left.php"); ?>
    <div id="middleContent">
        <?php
        if (isset($_GET['success']) && empty($_GET['success'])) {
echo 'You have been registered successfully';
} else {

    if (empty($_POST) === false && empty($errors) === true) {
        //echo "**********************";
        change_password($session_mem_id, $_POST['password']);
        header('Location: changepassword.php?success');
        exit();
    } else if (empty($errors) === false) {
        echo output_errors($errors);
    }

    ?>
        <form action="" method="post">
            <ul>
                <li>Current Password*: <br> <input type="password"
                    name="current_password">
                </li>
                <li>New Password*: <br> <input type="password" name="password">
                </li>
                <li>Repeat New Password*: <br> <input type="password"
                    name="password_again">
                </li>
                <li><input type="submit" value="Change">
                </li>
            </ul>
        </form>
        <?php }?>
    </div>
    <?php include ("includes/overall/column_right.php"); ?>
</div>
<?php include ("includes/overall/template_footer.php");?>
</body>
</html>

并且以防万一您需要查看更改密码功能:

function change_password($mem_id, $password) {
$mem_id = (int)$mem_id;

mysql_query("UPDATE `members` SET `mem_password` = '$password' WHERE `mem_id` = $mem_id");
}

数据库上的密码更新正常,只是纯粹不会重定向到成功页面。提前致谢

4

2 回答 2

0

标头指令必须位于内容、任何内容、包括换行符、空格、html 等之前……否则发送标头为时已晚。一旦发送了 1 位内容,标头就已经消失了。

于 2013-02-28T14:56:05.577 回答
-1

header('Location: changepassword.php?success');输出任何内容后都不能放。重定向也header应该包含绝对路径。

于 2013-02-28T14:51:28.850 回答