Warning: Cannot modify header information - headers already

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
-RiE-
Forum Newbie
Posts: 1
Joined: Wed Jul 02, 2008 8:14 pm

Warning: Cannot modify header information - headers already

Post by -RiE- »

Hello,
I create login pages using PEAR.
I got error code when I logging in.

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at ....\header.php:9) in ....\login.php on line 45
the login.php script:

Code: Select all

<?php
include ("include/header.php");
include ("HTML/QuickForm.php");
?>
    <div class="main">
        <div class="main-c">
            <div class="big">
                <div class="big-c">
                    <div class="blog-form">
                        <fieldset>
                            <legend>Login</legend>
                                <?php // initialize form object
                                $form = new HTML_QuickForm("login"); ?>
                                <?php // add text input boxes
                                $form->addElement("text", "username", "Username:", array("size" => 30));
                                $form->addElement("password", "password", "Password:", array("size" => 30));
                                // add submit button
                                $form->addElement("submit", null, "Submit");
                                $form->addElement("reset", null, "Reset");
                                // Run filter
                                $form->applyFilter(array("username", "password", "email"), "trim");
                                $form->applyFilter("username", "addslashes");
                                $form->applyFilter("username", "htmlentities");
                                $form->applyFilter("password", "addslashes");
                                $form->applyFilter("password", "htmlentities");
                                if ($form->validate()) {
                                    $form->freeze();
                                    // retrieve submitted data as array
                                    $data = $form->exportValues();
                                    // process input
                                    $username = $_POST['username'];
                                    $password = $_POST['password'];
                                    $password = md5($password);
                                    $login_query = "SELECT username FROM user WHERE username='$username'";
                                    $login_result = mysql_query($login_query);
                                    if (mysql_num_rows($login_result) == 1)
                                    {
                                        $login2_query = "SELECT username, password FROM user WHERE username='$username' AND password='$password'";
                                        $login2_result = mysql_query($login2_query);
                                        $row_login = mysql_fetch_assoc($login2_result);
                                        if ($row_login)
                                        {
                                            $_SESSION['auth'] = "yes";
                                            $_SESSION['logname'] = $row_login['username'];
                                            header ("Location: index.php"); //line 45
                                        }
                                        else
                                        {
                                            echo "You haven't entered correct password!";
                                        ?>
                                       
                        </fieldset>
                    </div>   
                </div>
            </div>
        <?php include ("include/sidebar.php"); ?>
        </div>
    </div>
 
<?php include ("include/footer.php");                           
                                        }
                                    }
                                    else
                                    {
                                        echo "The username you entered does not exist!";
                                    ?>
                        </fieldset>
                    </div>   
                </div>
            </div>
        <?php include ("include/sidebar.php"); ?>
        </div>
    </div>
 
<?php include ("include/footer.php");                  
                                    }
                                    exit;
                                }
                                echo "<p>Don't have any account? <a href=\"./signup.php\">Register Now!</a></p>";
                                echo "<p>Lost Password ? <a href=\"./forgotmypass.php\">Recover Your Password</a></p>";
                                $form->display(); ?>
                        </fieldset>
                    </div>   
                </div>
            </div>
        <?php include ("include/sidebar.php"); ?>
        </div>
    </div>
 
<?php include ("include/footer.php"); ?>
the header.php script:

Code: Select all

<?php
    session_start();
    include ("include/function.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php show_title("home"); //Line 9 ?></title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div class="content">
    <div class="content">
        <p class="topnav"><a href="./index.php">Home</a> &middot; <a href="./signup.php">Register</a> &middot; <a href="./login.php">Login</a></p>
        <h1><a href="index.php"><?php show_sitename(); ?></a></h1>
can someone help me?

thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Warning: Cannot modify header information - headers already

Post by John Cartwright »

Normally we discourage saying this.. but Google it! This is probably the most common php problem, ever.

But to be fair and answer your question, you cannot have any output before issuing a header()
Post Reply