0

ヘッダーの前に HTML 要素があると思われるため、ヘッダーが正しいページにリダイレクトされません。私は PHP が初めてで、ページを再配置するのに助けが必要なので、そうではありません。ヘッダー行を次のように置き換えると

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

それは完全に機能しますが、これは JS を使用するための良い習慣ではないことを知っています。1 つが表示されたら、私のウェブサイトでそれらのどれも機能しないので、残りはそうすることができます! 以下は私の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

出力バッファリングを使用できます。

PHP では、コンテンツの前にヘッダーを送信する必要があります。したがって、何かを出力すると( 、 などを含みechowhitespaceを含むブロックprintの外側にあるものはすべて)、ヘッダーを変更することはできません。<?php ?>

ただし、できることは、出力バッファリングをオンにすることです。これにより、すべての出力が一時バッファに送信されます。そのバッファは、指示するまで出力されません。

基本的なパターンは次のとおりです。

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 に答える
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 などの前に実行する必要があります。

つまり、「product of」header();機能は、ブラウザに送信される最初のデータでなければなりません。

于 2013-02-28T22:55:55.227 に答える