开发者

Massive newbie error while using the PHP header() function

I'm trying to redirect to a .php page if the $validForm variable is TRUE.

Here's my code:

<?php session_start();
require("validationLibrary.php");
$validForm = true;
?>

<html>
    <head>
        <title>Registration Form - 1 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 1 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>        
        <form action="registerFormOne.php" method="post">
           开发者_Python百科 <dt>First Name:</dt>
                <dd><input type="text" name="firstName" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['firstName'])){
                            if(!validateRequired($_POST['firstName'])){
                                $validForm = false;
                            }
                        }
                    ?>
                </dd><br />

            <dt>Last Name:</dt>
                <dd><input type="text" name="lastName" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['lastName'])){
                            if(!validateRequired($_POST['lastName'])){
                                $validForm = false;
                            }
                        }
                    ?>
                </dd><br />

            <dt>EMail:</dt>
                <dd><input type="text" name="email" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['email'])){
                            if(!validateEmail($_POST['email'])){
                                $validForm = false;        
                            }
                        }                        
                    ?>
                </dd><br />

            <dt>Age:</dt>
                <dd><input type="text" name="age" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['age'])){
                            if(!validateNumber($_POST['age'])){
                                $validForm = false;        
                            }
                        }                        
                    ?>
                </dd><br />

            <dt>Date of Birth:</dt>
                <dd><input type="text" name="dateOfBirth" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['dateOfBirth'])){
                            if(!validateRequired($_POST['dateOfBirth'])){
                                $validForm = false;        
                            }
                        }                        
                    ?>
                </dd><br />

            <dt>Gender:</dt>
                <dd>Masculino <input type="radio" value="M" name="gender" checked/> &nbsp;&nbsp;
                Femenino <input type="radio" value="F" name="gender" />
                </dd>            

            <dt><input type="submit" /></dt>

            <?php
                if($validForm = true){                    
                    header("Location: registerFormTwo.php");                    
                    exit;
                }
            ?>
        </form>
    </body>
</html>

I'm getting this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\XAMPP\xampp\htdocs\registerFormOne.php:18) in C:\XAMPP\xampp\htdocs\registerFormOne.php on line 84


HTTP headers must be sent before any content is.

Which means your calls to the header() function have to be done before you do any output (quoting) :

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.


A pretty standard way to achive that is to :

  • put all validation code at the beginning of your script, before any HTML output
  • that validation code will :
    • either redirect, using header if everything is OK
    • or set variables, to keep track of errors
  • and, only after that, you'll use the variables set before, to re-display your form, if that is necessary.


Headers must be sent before any information is printed to the page. You must not call header() before echoing the HTML.


Separate the form validation and HTML generation parts. Validate the form first, optionally redirect, otherwise generate the HTML.


You can't use header() after you put contents on the page -- you have to do so before (Any character outputted to the client will cause it to not work. The page has to be completely blank).

In other words...

<html>

Is already too much.

What you should do is check if the form input is valid before echoing any HTML, and if it is, use header() to redirect, otherwise echo the HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜