0

我的标题没有重定向到正确的页面,因为我相信它之前有 HTML 元素。我是 PHP 新手,需要帮助重新排列我的页面,所以情况并非如此。如果我将标题行替换为

print_r("<script>location.href='changepassword.php?success'</script>");

它工作得很好,但是我知道这不是使用 JS 的好习惯。一旦展示了一个,我就可以让其余的,因为我的网站上没有一个可以工作!下面是我的 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']) && isset($_GET['success'])) {
            echo 'You have been registered successfully';
        } else {
if(empty($_POST) === false && empty($errors) === true) {
change_password($session_mem_id, $_POST['password']);
header('Location: changepassword.php?success');
} 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>
4

4 回答 4

1

您的标题需要在任何输出之前出现。这包括你的包含。include ("storescripts/init.php");, include ("includes/overall/head.php");,include ("includes/overall/template_header.php");任何输出都会停止标题。

于 2013-02-28T22:51:38.720 回答
0

这种安排应该奏效。我们只是将更改密码移到您的if()任何文档上方。注意die()告诉 PHP 停止处理的命令。当我们指示浏览器在其他地方继续时,无需执行脚本的其余部分。

正如 Dan 在下面指出的那样,您需要确保您的include()d 文件不包含任何输出。

<?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';
    }
}

if(empty($_POST) === false && empty($errors) === true) {
    change_password($session_mem_id, $_POST['password']);
    header('Location: changepassword.php?success=1');
    die();
}
?><!DOCTYPE html>
<html>
<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']) && isset($_GET['success'])) {
            echo 'You have been registered successfully';
        } 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>
于 2013-02-28T22:54:17.410 回答
0

这不是答案,如果您不知道,可能会提供有用的信息。

要使用header()功能 - 您必须在任何 echo、print_r 等之前执行它。

换句话说——“产品”header();功能必须是第一个发送到浏览器的数据。

于 2013-02-28T22:55:55.227 回答
0

您可以使用输出缓冲

在 PHP 中,标头必须在内容之前发送。因此,一旦您输出任何内容(包括echoprint等,以及<?php ?>块之外的任何内容,包括whitespace),则无法修改标头。

但是:您可以做的是打开输出缓冲,这会将所有输出发送到临时缓冲区。在您告诉它之前,该缓冲区不会被输出。

基本模式是:

ob_start();             //Turn on output buffering at the 
                        //   beginning of your script

echo "Hello world!";    //Here would be your HTML document

if ($redirect)          //This would be your condition for redirection
    header('Location: otherpage.php');
else
   ob_flush();          //At the very end, send the buffer to the browser

有许多用于结束缓冲的选项。在这种情况下,我只是使用 刷新了输出ob_flush(),但您可以查看 PHP 文档以了解其他各种选项。

于 2013-02-28T22:56:46.677 回答