Warning: Cannot modify header information - headers already sent

Discussion in 'PHP' started by -RiE-, Jul 2, 2008.

  1. #1
    Hello,
    I create login pages using PEAR.
    I got error code when I logging in.
    
    Warning: Cannot modify header information - headers already sent by (output started at ....\header.php:9) in ....\login.php on line 45
    
    Code (markup):
    the login.php script:
    
    <?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"); ?>
    
    PHP:
    the header.php script:
    
    <?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>
    
    PHP:
    can someone help me?

    thanks
     
    -RiE-, Jul 2, 2008 IP
  2. 2slick

    2slick Peon

    Messages:
    73
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you cannot place the header function after any html, it should be called first.
     
    2slick, Jul 2, 2008 IP
  3. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unless in the first line (or somewhere in the beginning) of your script you will use:
    ob_start()

    and at the end of the script:
    ob_end_flush();
     
    Greg Carnegie, Jul 4, 2008 IP
  4. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What Greg_Carnegie said works. However, you might want to try removing the white spaces on the first lines of your header.php script, like this:

    <?php
    session_start();
    include ("include/function.php");
    ?>
    PHP:
    ... for some reason, if you have white spaces before the session variables it causes this error. It happened to me once and I fixed it that way.

    Try it and if it doesn't work then go for Greg's solution.
     
    Ikki, Jul 4, 2008 IP
  5. popol

    popol Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i think it needs to be before any output is sent.. if you do something like:
    hi!<?php
    ob_start();
    ?>
    you're still in trouble(even just outputting a space instead of 'hi' will give out an error)..
     
    popol, Jul 5, 2008 IP
  6. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Correct even something as simple as:
    <?php
    ob_start();
    ?>

    (white space before <?php it is not visible here i think) will cause problems.
     
    Greg Carnegie, Jul 5, 2008 IP