Web Hosting Talk







View Full Version : Cannot modify header information


Snitz
04-18-2007, 07:20 AM
I'm receiving this error whenever I login into the client area of a website I'm developing.

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\hanaemg.com\html2\en\includes\topinc.php:20) in C:\wamp\www\hanaemg.com\html2\en\index.php on line 48

Line 20 at topinc.php is pure html
And this is line 48 at index.php
header('location: clients.php?id='.$row->id.'&'.rand());

This is the only includes in index.php
include_once("includes/topinc.php");

And these are the includes of topic.php
include("includes/conn.php");
include("includes/functions.php");
include("includes/h.module.php");

Would please tell me what I'm doing wrong?

Xeentech
04-18-2007, 07:33 AM
If the HTML from line 20 is sent to the client before you try to set the header it will fail.

You need to set all your headers before output because that's how it is in HTTP/1.1, and PHP doesn't buffer your output waiting for headers..

Snitz
04-18-2007, 08:02 AM
I didn't quite understood,

This is the code of topinc.php

<? include("includes/conn.php");
include("includes/functions.php");
include("includes/h.module.php");

session_start();
session_register('george');
if ($george=="") header('location: login.php?nologin=1');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to Hanam Engineering Consulting</title>
</head>

<body background="images/bg.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>&nbsp;</td>
<td width="158" height="94">
<table width="158" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="60"><img src="images/tl.gif" width="60" height="94" /></td>
<td width="98" background="images/tlbg.gif">&nbsp;</td>
</tr>
</table></td>
<td background="images/bgtop.gif"><img src="images/logo.gif" width="304" height="94" /></td>
<td background="images/bgtop.gif">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><table width="158" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="38"><img src="images/tl2.gif" width="38" height="179"></td>
<td width="120" valign="top" bgcolor="#144EA0">&nbsp;</td>
</tr>
</table></td>
<td width="692" height="179"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="692" height="179" title="Hanam">
<param name="movie" value="header.swf" />
<param name="quality" value="high" />
<embed src="header.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="692" height="179"></embed>
</object></td>
<td bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><img src="images/ml.gif" width="158" height="127"></td>
<td height="127" bgcolor="#FFC700"><table width="692" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="94" height="127"><img src="images/mm.gif" width="94" height="127" /></td>
<td>&nbsp;</td>
</tr>
</table></td>
<td bgcolor="#FFC700">&nbsp;</td>
</tr>

And this is the code of index.php

<? include_once("includes/topinc.php");
$id = $_GET['id'];
if(!$id) {
$id = '1';}
?>
<tr>
<td>&nbsp;</td>
<td colspan="2" valign="top"><table width="850" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="110"><img src="images/space.gif" width="110" height="1"></td>
<td width="740" valign="top" bgcolor="#FFFFFF">

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<? // if ($id) { $id = '1'; ?>
<td width="1%" height="100%" valign="baseline" bgcolor="#EEF7F9">&nbsp;</td>
<td width="23%" height="100%" valign="baseline" bgcolor="#EEF7F9"><p><img src="images/bullet.gif" width="8" height="7"> <strong><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif">Client Login</font> </strong></p>
<?
$username = @$_POST['username'];
$password = @$_POST['password'];

$cookie_pass=$_COOKIE[COOKIE.'clt'];

//echo $cookie_pass;
if (!empty($logout)) session_destroy();
if (!empty($nologin)) $msg="No Session Available, Log in!";

if ($username!=""){
$autopass = $_POST['autopass']=='true';
if (!$autopass and $cookie_pass!='') setcookie (COOKIE.'clt',"",today()-3600);

#$objConn=mysql_connect($dbServer,$dbUser,$dbPass) or die("Could not connect to database. Reason: ".mysql_error());
#mysql_select_db($dbDatabase,$objConn) or die("Could not select database '$dbDatabase'");

$strSQL="SELECT * FROM users where username = '" . sqlencode($username) . "'";
$objRS=mysql_query($strSQL);

if ($password==$dummy_password) $password=$cookie_pass;
if (!$row=mysql_fetch_object($objRS)):
// header('location: index.php');
$msg = "Username not found";
elseif ($row->password != $password):
$msg = "Invalid password";
else:
$george = $username;
$thename = $row->name;
if ($autopass) setcookie (COOKIE.'clt',$password,time()+3600*24*30);
header('location: clients.php?id='.$row->id.'&'.rand());
endif;


}
?>
<form name="clientlogin" method="post" action="index.php">
<? echo $msg; ?>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">Username</font><br>
<label>
<input name="username" type="text" size="15">
</label><br>
<label>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif">Password</font><br>
<input type=password name=password value="<? if ($cookie_pass!='') echo $dummy_password ?>" size="15">
</label><br>
<label>
<input type=submit name=submit value=" Login ">
</label>
</form><? // } ?>
<br>
<?
$conn5 = mysql_query("SELECT * FROM news order by id desc Limit 1");
$rows5 = mysql_num_rows($conn5);
$ccnt5 = 0;
while ($rows5 > $ccnt5) {
$connlist5[$ccnt5] = mysql_fetch_array($conn5);?>
<img src="images/bullet.gif" width="8" height="7"> <a href="news.php?sid=<?=$connlist5[$ccnt5]['id'];?>"><strong><font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"><?=$connlist5[$ccnt5]['title'];?></font></strong></a> (<font size="-3"><?=date("d F Y h:i a", strtotime($connlist[$ccnt]['date']))?></font>) <br>
<font size="2" face="Georgia, Times New Roman, Times, serif"><?=cleanfile($connlist5[$ccnt5]['para'], 100);?>... <a href="news.php?sid=<?=$connlist5[$ccnt5]['id'];?>">[more]</a></font></td>
<? $ccnt5++; }?>
<td width="1%" height="100%" valign="top"><p>&nbsp;</p></td>
<td height="100%" valign="top"><p><img src="images/bullet.gif" width="8" height="7"> <strong><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif"><?php
echo getsectionname($id);
?></font> </strong></p>
<p><font size="2" face="Courier New, Courier, monospace">
<?php

echo getincfile($id);

$conn4 = mysql_query("SELECT * FROM para WHERE subcatid = '$id' ORDER BY id asc");
$rows4 = mysql_num_rows($conn4);
$ccnt4 = '0';
while ($rows4 > $ccnt4) {
$connlist4[$ccnt4] = mysql_fetch_array($conn4);

if ($connlist4[$ccnt4]['image']) {


if($connlist4[$ccnt4]['align'] == 'center') {?>


<div align="center"><img src="<?php echo $connlist4[$ccnt4]['image']; ?>"></div>
<?php
} else { ?>

<img src="<?php echo $connlist4[$ccnt4]['image']; ?>" align="<?php echo $connlist4[$ccnt4]['align']; ?>">
<?php

}
echo $connlist4[$ccnt4]['para'];
}

$ccnt4++; } ?>
</font></p> </td>
</tr>
<? mysql_close(); ?>
<? include("includes/bottominc.php"); ?>

Can you show me how am I sending it to client before setting the header please?

jimpoz
04-18-2007, 11:51 AM
That is the top of index.php. You display the first few rows of a table...
<? include_once("includes/topinc.php");
$id = $_GET['id'];
if(!$id) {
$id = '1';}
?>
<tr>
<td>&nbsp;</td>
<td colspan="2" valign="top"><table width="850" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="110"><img src="images/space.gif" width="110" height="1"></td>
<td width="740" valign="top" bgcolor="#FFFFFF">

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
Further down...

if ($username!=""){
$autopass = $_POST['autopass']=='true';
if (!$autopass and $cookie_pass!='') setcookie (COOKIE.'clt',"",today()-3600);

#$objConn=mysql_connect($dbServer,$dbUser,$dbPass) or die("Could not connect to database. Reason: ".mysql_error());
#mysql_select_db($dbDatabase,$objConn) or die("Could not select database '$dbDatabase'");

$strSQL="SELECT * FROM users where username = '" . sqlencode($username) . "'";
$objRS=mysql_query($strSQL);

if ($password==$dummy_password) $password=$cookie_pass;
if (!$row=mysql_fetch_object($objRS)):
// header('location: index.php');
$msg = "Username not found";
elseif ($row->password != $password):
$msg = "Invalid password";
else:
$george = $username;
$thename = $row->name;
if ($autopass) setcookie (COOKIE.'clt',$password,time()+3600*24*30);
header('location: clients.php?id='.$row->id.'&'.rand());
endif;


}


Also, check to see that you're not sending the Location header twice. Try changing
if ($george=="") header('location: login.php?nologin=1');
to
if ($george=="") {
header('location: login.php?nologin=1');
exit;
}

Jatinder
04-18-2007, 01:15 PM
Add ob_start() at the very beginning of your topinc.php file.


<?php
ob_start();
include("includes/conn.php");
include("includes/functions.php");
include("includes/h.module.php");