无法修改标头信息 - 已经发送的标头:PHP

时间:2016-02-08 15:06:05

标签: php html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title></title>
    </head>
    <body>
<?php
    if ($_POST['password'] != $_POST['confirmpassword'])
    {        
        $password = md5($_POST['password']);
        $confirmPass = md5($_POST['confirmpassword']);        
        echo "<script type='text/javascript'>alert('Error. Passwords do not match.');</script>";    
        header("Refresh:0; Registration.html"); 
    }
    else 
    {
        $password = md5($_POST['password']);
        $confirmPass = md5($_POST['confirmpassword']);
        echo 'Name: '.$_POST['firstname'].' '.$_POST['lastname']. '<br>';

        if($_POST['agerange'] == 1)
        {
            echo "Under 18<br>";
        }
        elseif($_POST['agerange'] == 2)
        {
            echo "Age 18-24<br>";
        }
        elseif($_POST['agerange'] == 3)
        {
            echo "Age 25-34<br>";
        }
        elseif($_POST['agerange'] == 4)
        {
            echo "Age 35-44<br>";
        }
        elseif($_POST['agerange'] == 5)
        {
            echo "Age 45-54<br>";
        }
        elseif($_POST['agerange'] == 6)
        {
            echo "Age 55-64<br>";
        }
        else
        {
            echo "Age 65 or older<br>";
        }

        if ($_POST['sex'] == 'male')
        {
            echo "Gender: Male<br>";
        }
        else
        {
            echo "Gender: Female<br>";
        }

        echo 'Phone Number: '.$_POST['daytimephone']. '<br>';
        echo 'Email: '.$_POST['email']. '<br>';
        echo 'Username: '.$_POST['username']. '<br>';

        if(isset($_POST['specialoffers']))
        {
            echo "You would like to recieve special offers from us via email.";
        }
        else
        {
            echo "You would NOT like to recieve special offers from us via email.";
        }
    }
?>  
    </body>
</html>

所以这个PHP脚本调用我的Registration.html表单。如果密码字段与确认密码字段不匹配,则会发布一条警告,说明它们不匹配,然后应刷新页面。出于某种原因,我无法弄清楚它为什么会出现这个错误: 无法修改标题信息 - 已在第14行的Documents \ My Web Sites \ Week 2 \ Exercise 4 \ Register.php中发送的标题(在Documents \ My Web Sites \ Week 2 \ Exercise 4 \ Register.php:8中开始输出) 有没有人建议如何解决这个问题?如果密码字段不匹配,我想要的是刷新注册表单。

1 个答案:

答案 0 :(得分:0)

在修改标题之前,您无法输出任何内容。这意味着:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title></title>
    </head>
    <body>

而且:

 echo "<script type='text/javascript'>alert('Error. Passwords do not match.');</script>";  

不好。

除了其他人提到的安全方面之外,确保两个密码匹配的最佳方法是在使用jQuery validate或类似的东西提交表单之前。

如果必须这样做,最好将消息存储在会话变量中,然后将其输出到另一个页面上:

if ($_POST['password'] != $_POST['confirmpassword'])
    {        
        $password = md5($_POST['password']);
        $confirmPass = md5($_POST['confirmpassword']);        
        $_SESSION["msg"] =  "Error. Passwords do not match.";
        header("Location: Registration.php");
    }

在Registration.php上:

if (isset($_SESSION["msg"])){

echo "<script type='text/javascript'>alert('" . $_SESSION["msg"] . "');</script>";
unset($_SESSION["msg"]);
}