Hi,

I keep getting this error message.
Warning: Cannot modify header information - headers already sent by (output started at /home1/lgmsevze/public_html/passion4anime/test/header.php:18) in /home1/lgmsevze/public_html/passion4anime/test/login2.php on line 31

Can sumone help me or explain to me on how to fix this issue.. I will post my codes here...

Header:

<?php
session_start();
if(isset($_SESSION['SESS_CHANGEID']) == TRUE)
  {
session_unset();
session_regenerate_id();
  }
require("config.php");
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
?>
<!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=utf-8" />
<link rel="stylesheet" type="text/css" href="passion.css" /> 
<link rel="stylesheet" type="text/css" href="style.css" /> 
<link href="facebox.css" media="screen" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" />
<title>Passion 4 Anime - Watch your favorite Anime, Drama, Movies and more right here</title>
  <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
  <script type="text/javascript">
function mask(f){
tel='';
var val =f.value.split('');
for(var i=0;i<val.length;i++){
if(i==0){val[i]='('+val[i]}
if(i==2){val[i]=val[i]+')'}
if(i==5){val[i]=val[i]+'-'}
tel=tel+val[i]
}
f.value=tel;
}
</script>
 <script src="jquery.js" type="text/javascript"></script>
  <script src="facebox.js" type="text/javascript"></script>
  <script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox({
        loading_image : 'loading.gif',
        close_image : 'closelabel.gif'
      })
    })
  </script>
  <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
		$(document).ready(function(){
			$("a[rel^='prettyPhoto']").prettyPhoto();
		});
	</script>
    <script src="divswap.js" type="text/javascript"></script>
    <script src="show_hide.js" type="text/javascript"></script>
		<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
		<!--script src="js/jquery.lint.js" type="text/javascript" charset="utf-8"></script-->
		<link rel="stylesheet" href="css/prettyPhoto.css" type="text/css" media="screen" title="prettyPhoto main stylesheet" charset="utf-8" />
		<script src="js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
		
</head>

<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
<div align="center">
	<table width="900" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="160" class="p4a_banner"></td>
    <td class="php_header_p"><p> <a href="index.php" class="php_header_p">home</a> <a href="manga.php" class="php_header_p">manga</a> <a href="anime.php" class="php_header_p">anime</a> <a href="wallpapers.php" class="php_header_p">wallpapers</a> <a href="dramas.php" class="php_header_p">dramas</a></p></td>
    
     <?php
	  if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
	//Adding the Your Shopping Cart - # of Items and Total HERE
	echo "<td align='left' valign='middle' class='login_td'><p>Hi, <strong>" . ucwords($_SESSION['SESS_USERNAME']) . "</strong> [<a href='logout.php'>Logout</a>]<br/></p>"; 
	}
	  else {
	  		echo "<td align='left' valign='top' class='login_td'><p>
      <form action=\"login2.php\" method='post'><div class='login'><input type='text' value='Username' onclick=\"if(this.value=='Username')this.value='';\" onblur=\"if(this.value=='')this.value='Username';\" name=\"userBox\" class='login_input' /><div class='login'><input type='password' value='Password' onclick=\"if(this.value=='Password')this.value='';\" onblur=\"if(this.value=='')this.value='Password';\" name=\"passBox\" class='login_input' /><div><input style='background: url(img/login_btn.jpg) no-repeat; border:0;height:22px;width:56px;margin-left:75px;' value='' type='submit' name='submit'></div></div></div></form></p></td>";
	  }
		?>
    
    
    
  </tr>
</table>
  <table width="900" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td class="border"></td>
    </tr>
  </table>

Here is the Login.php

<?php
require ("header.php");
  ?>
  

  
  <?php
  if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
    header("Location: " . $config_basedir);
  }
  
  if(isset($_POST['submit']))
  {
    $loginsql = "SELECT * FROM lgmsevze_passion.logins
    WHERE username = '" . $_POST['userBox'] . "' AND password = '" . $_POST['passBox'] . "'";
    $loginres = mysql_query($loginsql);
    $numrows = mysql_num_rows($loginres);
    
    if($numrows == 1)
    {
      $loginrow = mysql_fetch_assoc($loginres);
      
      session_register("SESS_LOGGEDIN");
      session_register("SESS_USERNAME");
      session_register("SESS_USERID");
      
      $_SESSION['SESS_LOGGEDIN'] = 1;
      $_SESSION['SESS_USERNAME'] = $loginrow['username'];
      $_SESSION['SESS_USERID'] = $loginrow['id'];
      
      $ordersql = "SELECT id FROM lgmsevze_passion.orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2";
        $orderres = mysql_query($ordersql);
        $orderrow = mysql_fetch_assoc($orderres);
        
        session_register("SESS_ORDERNUM");
        $_SESSION['SESS_ORDERNUM'] = $orderrow['id'];
        
          header("Location: " . $config_basedir);
        }
        else
        {
          header("Location:");
            }
      }
      
      else
      {
         
        
    
      
      
  ?>        <img src="shana.jpg" width="900" height="308" />
  <table width="900" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align="left" valign="top"  class="top_series"><p><b>Top Anime Series:</b> <a href="" class="topseries">Angel Beats!</a>, <a href="" class="topseries">Death Note</a>, <a href="" class="topseries">Blood +</a>, <a href="" class="topseries">Elfen Lied</a>, <a href="" class="topseries">Code Geass</a></p></td>
    </tr>
  </table>  <table width="900" height="500" bgcolor="#FFFFFF" >
  	<tr>
    <td width="200" bgcolor="#eeeeee"></td>
    	<td valign="top" class="table_middle"><span class="correct">
  <?php  //add on delete if problems occur
  switch(isset($_GET['correct'])) {
      case "crt":
          echo "Your username and password has been successfully created.";
        break;
    }
    //end of add on
    ?>  
      </span>  
    <h1>Customer Login</h1>
    <p>Please enter your username and password to log into the websites.<br />If you do not have an account, you can get one for free by <a href="register.php">registering</a>.</p>
 
            <?php
        if(isset($_GET['error'])) {
          echo "<strong>Incorrect username/password</strong>";
          }
       ?>
     
          
          <form action="login.php" method="POST">
  <table>
    <tr>
      <td>Username</td>
      <td><input type="textbox" name="userBox"></td>
      </tr>
      <tr>
        <td>Password</td>
        <td><input type="password" name="passBox"></td>
      </tr>
      <tr>
        <td></td>
        <td><input type="submit" name="submit" value="Log in"></td>
      </tr>
      </table>
      </form>
      </td>
         </tr>
         </table>
          
          
          <?php
          }
          require("footer.php");
          ?>

Here is the Login2.php

<?php
  require ("header.php");
  if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
   echo "";
  } 
  if(isset($_POST['submit']))
  {
    $loginsql = "SELECT * FROM lgmsevze_passion.logins
    WHERE username = '" . $_POST['userBox'] . "' AND password = '" . $_POST['passBox'] . "'";
    $loginres = mysql_query($loginsql) or die (mysql_error());
    $numrows = mysql_num_rows($loginres)or die (mysql_error());
    
    if($numrows == 1)
    {
      $loginrow = mysql_fetch_assoc($loginres);
      
      session_register("SESS_LOGGEDIN");
      session_register("SESS_USERNAME");
      session_register("SESS_USERID");
      
      $_SESSION['SESS_LOGGEDIN'] = 1;
      $_SESSION['SESS_USERNAME'] = $loginrow['username'];
      $_SESSION['SESS_USERID'] = $loginrow['id'];
      
      $ordersql = "SELECT id FROM lgmsevze_passion.orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2";
        $orderres = mysql_query($ordersql);
        $orderrow = mysql_fetch_assoc($orderres);
        
        session_register("SESS_ORDERNUM");
        $_SESSION['SESS_ORDERNUM'] = $orderrow['id'];
          header("Location: ");
        }
        else
        {
          header("Location: ");
            }
      }
      else
      {
	  echo "";
          }
?>
<?php	  
require ("footer.php");
?>

Dani AI

Generated

This warning means PHP tried to send a header after output had already been emitted. The traceback in your post points to output starting in header.php (line 18), so any later calls to header() in login.php/login2.php fail. As advised, the usual, clean fix is to perform authentication and redirects before you include or print any HTML; as noted, even blank spaces or a BOM will count as output.

A practical pattern: handle POST/authentication at the top of the script, redirect, then include the page header. Example (not a copy of your files):

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // validate credentials (use prepared statements)
    if ($valid) {
        $_SESSION['SESS_LOGGEDIN'] = 1;
        header('Location: /test/index.php');
        exit;
    } else {
        header('Location: /test/login.php?error=1');
        exit;
    }
}

require 'header.php';

If you cannot restructure immediately, ob_start() will buffer output and avoid the warning, but it’s a workaround, not a best practice:

<?php
ob_start();
require 'header.php';
// later: header('Location: ...'); exit;
ob_end_flush();

Quick troubleshooting checklist:

  • Inspect header.php at the reported line for the DOCTYPE/HTML output — that output happens before your redirects.
  • Move form-processing/redirect logic before any include that emits HTML.
  • Ensure header("Location: ...") is plain text (no HTML tags) and follow it with exit;
  • Remove trailing PHP closing tags in pure-PHP files and save files as UTF-8 without BOM to avoid hidden bytes.
  • Replace deprecated session_register() with direct $_SESSION assignments.
  • Turn on errors (error_reporting(E_ALL); ini_set('display_errors',1);) while debugging.

Following the “logic first, presentation after” approach will fix the error and make the code easier to maintain.

Recommended Answers

All 3 Replies

When you output things you send headers response to the visitor’s browser. If you have done that you can’t change your mind and send other header.

In header.php (witch you require at the second line of Login.php) you start output (also with html code) then in the continue of Login.php you have many cases where you send deferent headers (e.g. header("Location: " . $config_basedir); )

The simplest solution is to require (witch is used as in include in this case) after the cases check.

When you output things you send headers response to the visitor’s browser. If you have done that you can’t change your mind and send other header.

In header.php (witch you require at the second line of Login.php) you start output (also with html code) then in the continue of Login.php you have many cases where you send deferent headers (e.g. header("Location: " . $config_basedir); )

The simplest solution is to require (witch is used as in include in this case) after the cases check.

Headers may not be sent after any script or HTML output. Blank spaces are also output.
Your output starts at
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
in header.php
Rearrange your code so that no output precedes the header() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.