PDA

View Full Version : Need help with PHP "Warning: Cannot modify header information"


bikoalive
05-20-2007, 12:13 PM
I get this message.

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/question.php:10) in /home/username/public_html/question.php on line 30

What is the problem? Please help me. Thank you so much in advance.
Here is the script I wrote.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hello</title>
</head>
<body background="bg.jpg" style="margin:0">
<table border="0" width="100%" height="100%" cellspacing="0" cellpadding="0">
<tr align="center">
<td>
<p><font size="5" face="arial"><?php if(strpos($_POST['question'],'?'))
{
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("mydata1");
$datTim = $_REQUEST['datTim'];
$dt = $_REQUEST['dt'];
$ip = $_REQUEST['ip'];
$question = $_REQUEST['question'];
if( $id )
{
$query = "UPDATE `comments` SET `datTime`='$datTim', `dt`='$dt', `ip`='$ip', `question`='$question' WHERE `id`='$id'";
}
else
{
$query = "INSERT INTO `comments` ( `datTim`,`dt`,`ip`,`question` )
VALUES ( '$datTim','$dt','$ip','$question' )";
}
$results = mysql_query( $query );
if( $results )
{
header('Location: /index.php');
}
else
{
die( "There is an error. Please contact me: " . mysql_error() );
}
}
else
{
echo ( "Do you really know that????" );
}
?>
<br>
<br>
<br><a href="javascript:history.go(-1)">Back</a></font> </p></td>
</tr>
</table>

Pethens
05-20-2007, 12:20 PM
As soon as you generate any HTML output, PHP automatically sends the headers. After that point it's not possible to send any more headers, so your "Location:" header causes the error. Try this instead, or move the block of HTML wherever your logic requires, but after the "Location:" header:

<?php if(strpos($_POST['question'],'?'))
{
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("mydata1");
$datTim = $_REQUEST['datTim'];
$dt = $_REQUEST['dt'];
$ip = $_REQUEST['ip'];
$question = $_REQUEST['question'];
if( $id )
{
$query = "UPDATE `comments` SET `datTime`='$datTim', `dt`='$dt', `ip`='$ip', `question`='$question' WHERE `id`='$id'";
}
else
{
$query = "INSERT INTO `comments` ( `datTim`,`dt`,`ip`,`question` )
VALUES ( '$datTim','$dt','$ip','$question' )";
}
$results = mysql_query( $query );
if( $results )
{
header('Location: /index.php');
}
else
{
die( "There is an error. Please contact me: " . mysql_error() );
}
}
else
{
echo ( "Do you really know that????" );
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hello</title>
</head>
<body background="bg.jpg" style="margin:0">
<table border="0" width="100%" height="100%" cellspacing="0" cellpadding="0">
<tr align="center">
<td>
<p><font size="5" face="arial">
<br>
<br>
<br><a href="javascript:history.go(-1)">Back</a></font> </p></td>
</tr>
</table>

bikoalive
05-20-2007, 12:32 PM
Thanks for your quick response.
I fixed it to your version and still get the message below.

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/question.php:1) in /home/username/public_html/question.php on line 21

Any other solution?

felgall
05-20-2007, 12:46 PM
You must still have something being written to the page before trying to write the header. Possibly a blank line or something before the <?

bikoalive
05-20-2007, 12:50 PM
Thank you Stephen,

I don't think so. The script is just as you can see above. I use Notepad and made sure that there is no blank line on the top.

TinyLnk
05-20-2007, 01:19 PM
<?php if(strpos($_POST['question'],'?'))
{
//$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
//mysql_select_db ("mydata1");
$datTim = $_REQUEST['datTim'];
$dt = $_REQUEST['dt'];
$ip = $_REQUEST['ip'];
$question = $_REQUEST['question'];
if( $id ) {
$query = "UPDATE `comments` SET `datTime`='$datTim', `dt`='$dt', `ip`='$ip', `question`='$question' WHERE `id`='$id'";
} else {
$query = "INSERT INTO `comments` ( `datTim`,`dt`,`ip`,`question` ) VALUES ( '$datTim','$dt','$ip','$question' )";
}
//$results = mysql_query( $query );
$results = TRUE;
if( $results ) {
header('Location: /index.php');
} else {
die( "There is an error. Please contact me: " . mysql_error() );
}
}
else
{
echo ( "Do you really know that????" );
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hello</title>
</head>
<body background="bg.jpg" style="margin:0">
<table border="0" width="100%" height="100%" cellspacing="0" cellpadding="0">
<tr align="center">
<td>
<p><font size="5" face="arial">
<br>
<br>
<br><a href="javascript:history.go(-1)">Back</a></font> </p></td>
</tr>
</table>


I just edited out some sql stuff and tested, works fine for me.
You are submiting directly to it right?

bikoalive
05-20-2007, 01:30 PM
Hello TinyLnk,

Thank you for your support also,
but I still get the message:( :

Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/question.php:1) in /home/username/public_html/question.php on line 17

TinyLnk
05-20-2007, 01:33 PM
and theres nothing above the <?php line ? :(

TinyLnk
05-20-2007, 01:36 PM
are you submitting directly to the script?

bikoalive
05-20-2007, 01:37 PM
100% certain. and yes :(

TinyLnk
05-20-2007, 01:41 PM
ok.. I know your problem.. you have whitespace before the <?php tag

that means you have " <?php" instead of "<?php" so delete the spaces before that and it will be fine. the space is getting sent to the browser, and when anything goes to the browsers the headers must go first.

so just delete the space and your problem is solved.

bikoalive
05-20-2007, 01:46 PM
where? the script is just you can see. There is no space nor line before "<?php"

TinyLnk
05-20-2007, 01:51 PM
Nothing at all before it? Only way I get that error is if I put a space(or any other character) before the "<" anything at all before the "<" will cause headers to be sent to the browser, making the Location header an error.

TinyLnk
05-20-2007, 02:01 PM
can you put a link to the script ?

bikoalive
05-20-2007, 02:04 PM
you mean putting the link to the location part?
I tried that but the same thing happens. I am getting so desperate. Nothing like this happend when I was using another web hosting server. maybe PHP version's upper on bluehost? darn. i want to cry.

Basil
05-20-2007, 02:06 PM
He wants you to link us to your script.

TinyLnk
05-20-2007, 02:27 PM
guess he dont want to give the link -.-

bikoalive
05-20-2007, 02:28 PM
Oh guys, it was not that....In the Notepad, I saved the file with UTF-8 encoding. Now I've changed to ANSI encoding and it works. I thought PHP supports UTF-8. How come this happens?? I am shocked. :eek:

Basil
05-20-2007, 02:36 PM
Bluehost stopped supporting UTF-8 I think, that's why all the foreign characters in emails were getting screwed up.

bikoalive
05-20-2007, 02:38 PM
What?? are you serious?? I just paid for 2 years package. maybe I need to cancel. UTF-8 is extremely necessary for me.

Basil
05-20-2007, 02:48 PM
I think the upgrade from php 4.4.5 to 4.4.6 is what caused it.

A proposed fix for it is to add this to a php.ini file..
From: http://helpdesk.bluehost.com/kb/index.php?x=&mod_id=2&id=432
[mbstring]
mbstring.language = Neutral
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.http_output = UTF-8
mbstring.encoding_translation = On
mbstring.detect_order = auto
mbstring.substitute_character = long;
mbstring.func_overload = 1

This is apparently for emails, I don't know if it'll help or not.

bikoalive
05-20-2007, 02:54 PM
Okay I've been chatting with Bluehost support and this person explained as follows.

Bluehost: we are trying to fix that on our end
Bluehost: there is a bug and our devoloper are aware of it
Me: what is wrong specifically? and how long is it giong to take?
Bluehost: I wouldn't know, but our developer are working on it
Bluehost: my guess would be within a couple month

bikoalive
05-20-2007, 03:05 PM
darn, i can't wait for months. I may need to cancel Bluehost. Is this a PHP new version's bug or Bluehost's?

Basil
05-20-2007, 03:10 PM
I'm not really sure, did you try putting that code into a php.ini file in the folder with your script?

TinyLnk
05-20-2007, 03:39 PM
would the utf8_encode() and utf8_decode() functions work for you ?

I dont know what your doing so I'm just throwing stuff out there for ya ;)

Pethens
05-20-2007, 03:58 PM
Before you give up on Bluehost, change your source file back to utf-8 and change this setting in your php.ini:

default_charset = "UTF-8"

Knowldedgebase article is here:
http://helpdesk.bluehost.com/kb/index.php?x=&mod_id=2&id=415

Pethens
05-20-2007, 04:11 PM
I created a UTF-8 file with notepad and published it without any problems, with or without the default_charset setting in php.ini. Maybe there is something else going on with your script?

TinyLnk
05-20-2007, 04:17 PM
yea, but you didnt try changing the header.

Pethens
05-20-2007, 05:19 PM
Darn, you're right. But looking at those notepad-created utf-8 files in another text editor shows what the problem is: notepad automatically inserts a byte order marker at the beginning of utf-8 files. This comes before the <?php and is just as if you had some stray white space there. The solution is to use a text editor that doesn't insert garbage into your files.

See this PHP bug report:
http://bugs.php.net/bug.php?id=22108

TinyLnk
05-20-2007, 06:24 PM
yea, I noticed that too. Dono what its there for.

bikoalive
05-20-2007, 09:52 PM
Pethens and TinyLnk,

Thank you very much for the report on text editor. I am glad you found it and I am not the only loser :p .


:( turned out to be :eek: :eek:

Basil
05-20-2007, 10:08 PM
If you're looking for a good text editor I recommend crimson editor (http://crimsoneditor.com).

bikoalive
05-20-2007, 10:19 PM
with crimson editor, i guess i need to "utf-8 encoding WITHOUT BOM" right?

Basil
05-20-2007, 10:26 PM
The BOM is probably what notepad was adding to the beginning of the file that was making it so you couldn't send the headers. So yeah, go without the BOM.

TinyLnk
05-20-2007, 11:04 PM
http://en.wikipedia.org/wiki/Byte_Order_Mark

bikoalive
05-23-2007, 03:11 PM
Basil, I am using crimson editor, right. Okay, I change the format to UTF-8 without BOM. Click save and close the document. When I open it again, the document is ANSI. Do you know what's going on?

Basil
05-23-2007, 05:15 PM
The BOM tells the editor what encoding the document is using, without it I believe crimson will use the default, however I think the document is still read as UTF-8 on the server.