Warning: Cannot modify header information - headers alre...

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
biggie
Forum Newbie
Posts: 8
Joined: Fri May 26, 2006 11:57 pm

Warning: Cannot modify header information - headers alre...

Post by biggie »

arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a simple form in a html with a process.php file like:

Code: Select all

<?php
include("global.inc.php");
$errors=0;
$error="Some error message.<ul>";
pt_register('POST','CNP');
pt_register('POST','Nume');
pt_register('POST','Prenume');
if($CNP==" "){
$errors=1;
$error.="<li>Error...";
}
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
$error.="<li>invalid email address";
$errors=1;
}
if($errors==1) echo $error;
else{
$link = mysql_connect("localhost","user","password");
mysql_select_db("db",$link);

$idtest=$_POST[id];
$quer = "SELECT id FROM pasagger WHERE id=$idtest";
$rez = mysql_query($quer);

if(mysql_num_rows($rez)) {
 echo "Duplicate ID";
} else {

$query="insert into pasager (cnp,nume,prenume) values ('".$CNP."','".$Nume."','".$Prenume."')";
mysql_query($query);
}
header("Location: http://www.google.com/");
?><?php 
}
?>


as u can see there is a if testing for duplicate id...if in the form I put an id that already is in the database then I will get the error (Duplicate ID) but under I also get

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\nusphere\techplat\apache\htdocs\site\pazbor.php:31) in c:\program files\nusphere\techplat\apache\htdocs\site\pazbor.php on line 33(the line with header(...)


why? what am I doing wrong?


arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I suspect looking at the code that it is the line:

Code: Select all

echo "Duplicate ID";
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

But your page on another page like includedpage.php
and than put this on yourpage you have now

Code: Select all

<?php
ob_start();
include("includedpage.php");
ob_flush();
?>


That should solve the message no matter what but you can try doing this first (on your current page):

Code: Select all

<?php
ob_start();

// Codes 

ob_flush();
?>

The ob_start(); I used on both examples, starts putting the codes into the memory, and then the ob_flush(); sends all of the items in memory to the client. ob_flush is called automatically at the end of every php code, but its better just to call it and be on the safe side.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Also with:

Code: Select all

if(mysql_num_rows($rez)) { 
 echo "Duplicate ID"; 
} else {
It would be a little safer just do go ahead and make an arrugment:

Code: Select all

if(mysql_num_rows($rez)>0) { 
 echo "Duplicate ID"; 
} else {
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Tecktalkcm0391, suggesting output buffering is a band-aid approach to fixing "header already sent" issues. It's just not suggested, let alone just hiding the real problem.

It'd be a good idea to read this thread: viewtopic.php?t=1157
Post Reply