PHP Header Error?

Hi Everyone,

Everytime I log into my website I always get the below error.

Cannot modify header information - headers already sent by ......

I thought maybe if my form submitted to another page it would work but it didn’t.
I thought if I echo’ed out the header information that would work but it didn’t. (instead of including it)

Below is my syntax. What am I doing wrong?

<?php
session_start();
include"connect_to_mysql3.php";
$loggedinuser = $_SESSION["id"];
$name = $_SESSION["name"];

include 'header.php';

/* echo '
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My Dynamic Website</title>

<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
    <div id=container>
	
            <h1>My Dynamic WebSite</h1>
			
        <nav>
            <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="variables.php">Variables</a></li>
                <li><a href="arrays.php">Arrays</a></li>
                <li><a href="functions.php">Functions</a></li>

            </ul>
        </nav>'; */

/* include("edit-car-function-for-login2.php");

if (isset($_POST['name'], $_POST['password'])) {

$name = $_POST['name'];
$password =$_POST['password'];
loggeduser($name, $password);

} */

// $page = isset($_GET['page']) ? $_GET['page'] : 'home';

if (isset( $_POST['name'], $_POST['password'] )){

    $name = $_POST['name'];
	$password = $_POST['password'];
	
	$password = sha1($password);
	
	$name = mysql_real_escape_string($name);
    $password = mysql_real_escape_string($password);
	
	$sql_id = mysql_query("SELECT * FROM practice WHERE name='$name' AND password='$password'");
    while($row = mysql_fetch_array($sql_id)){
    $id = $row["id"];
    }
	
	$sql = ("SELECT id FROM practice WHERE name='$name' AND password='$password'");

	$result = mysql_query($sql);
	
	$count=mysql_num_rows($result);
	
	if($count==1){


    $_SESSION["name"] = $name;
    $_SESSION["id"] = $id;

   header("location:heythere.php");

   } else {
      echo "Wrong Username and Password.";
  }
}


?>

<?php
// include 'header.php';
?>

<?php

  if ($loggedinuser != 0){

  echo "Hi $name, your logged in! <br>";

  echo "<p><a href=\\"http://whatsmyowncarworth.com/class-work/sign3/logout.php\\">Log Out</a></p>";


} else {


  echo '<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="valchecklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><center><strong>Member Login </strong></center></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>


<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>

</form>
</tr>
</table>';



}
?>

<?php
include 'footer.php';
?>

you cannot send a header() call after you’ve echoed or sent anything (HTML, Whitespace, etc) out to the browser.

Hey, thanks for the reply and help but I’m still not site as to how to actually fix it?

Check your header.php for empty lines after your closing ?> or any echo statements.
Once that is cleared up, verify you do not have any empty lines or echo statements being executed prior to your header() call.

Hey cpradio,

Thanks for your help.

Below is my header.php file with no spaces or php. It’s still not working.

<link rel="stylesheet" type="text/css" href="style.css">
<div id="container">
 <h1>Cars</h1>
<nav>
<ul>
   <li><a href="valchecklogin.php">Home</a></li>
   <li><a href="heythere.php">Profile</a></li>
   <li><a href="display-inventory.php">Cars</a></li>
   <li><a href="mygoal.php">Goal</a></li>
   <li><a href="add-car.php">Car+</a></li>
   <li><a href="add-dealership.php">Dealership+</a></li>
   <li><a href="edit-car.php">Edit Cars</a></li>
   <li><a href="cal.php">Cal</a></li>
   <li><a href="forgot2.php">Forgot Pass</a></li>
   <li><a href="change-password.php">Change Pass</a></li>
   <li><a href="register.php">Reg</a></li>
   <li><a href="contact.php">Contact</a></li>
   <li><a href="logout.php">Log Out</a></li>
</ul>
</nav>

It may not have any PHP, but it has output, ALL of that HTML CANNOT be before your header() call. So your include ‘header.php’; being prior to your header() call is your issue.

You need to include the file after your header() calls.

Here is a bit more technical background to header()

It may not have any PHP, but it has output, ALL of that HTML CANNOT be before your header() call. So your include 'header.php'; being prior to your header() call is your issue.

You need to include the file after your header() calls.

Hey cpradio,

Thanks for your response. It’s helped me out plenty but I still have one small minor issue.

When a user types in there incorrect user/pass the error message they get is above the header.php. “Wrong Username and Password.” is in the black of the CSS.

It it possible to move this error message to where the user can see it in a much more effective way?

Thanks Everyone!

store it in a variable, check if the variable is not empty($variable) and then write it to the page where you want it.

Perfect cpradio!

It’s working great! Thanks again!