homepage Welcome to WebmasterWorld Guest from 54.197.171.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Cannot modify header information
How annoying!
Sub_Seven




msg:4252654
 1:00 am on Jan 13, 2011 (gmt 0)

I need help from one of the gurus again

I'm getting this message after sending data to mysql:

Successfully added
Warning: Cannot modify header information - headers already sent by (output started at /home/domainhere/public_html/sub/includes/gc_insert.php:1) in /home/domainhere/public_html/sub/includes/gc_insert.php on line 24

This is my code:
<?php
$con = mysql_connect("localhost","user","pass") or die("Could not connect:".mysql_error());

mysql_select_db("db", $con);

$sql = "INSERT INTO comments (status, name, city, state, zipcode, country, comment)
VALUES (
'". mysql_real_escape_string(htmlspecialchars($_POST['status']))."',
'". mysql_real_escape_string(htmlspecialchars($_POST['name']))."',
'". mysql_real_escape_string(htmlspecialchars($_POST['city']))."',
'". mysql_real_escape_string(htmlspecialchars($_POST['state']))."',
'". mysql_real_escape_string(htmlspecialchars($_POST['zipcode']))."',
'". mysql_real_escape_string(htmlspecialchars($_POST['country']))."',
'". mysql_real_escape_string(htmlspecialchars($_POST['comment']))."')";

if (!mysql_query($sql,$con)){
echo "there was an error";
}
else{
echo "Successfully added";
}

header('Location: index.php');

mysql_close($con);
?>


I have read about this error and I've made sure there are no white spaces anywhere, I've put the header line before displaying anything to the browser (echo) and the error still appears.

If I run var_dump(headers_list()); I get the following

array(2) { [0]=> string(24) "X-Powered-By: PHP/5.2.15" [1]=> string(23) "Content-type: text/html" }

But I don't see anything useful there that could help me fix the issue.

I know of a workaround which is using something like:

<meta http-equiv="refresh" content="1;URL=/index.php">

But I'd hate to use that, I just don't like it and I have other pages where I've used a header with no problem, I just don't know why in this case this is happening.

Any suggestions will be much appreciated, thanks everyone.

 

topr8




msg:4252657
 1:06 am on Jan 13, 2011 (gmt 0)

>>echo "Successfully added";

once you've written to the page you can no longer send headers

Sub_Seven




msg:4252670
 1:31 am on Jan 13, 2011 (gmt 0)

Ok, I have removed the if statements containing the echo lines and I get the same error...

aakk9999




msg:4252673
 1:51 am on Jan 13, 2011 (gmt 0)

Perhaps try to remove the other
echo "there was an error";

<added>
Is this code of the script to which error refers to? I.e. if this is include, maybe headers were sent previously?
Or the actual error is in fact in piece of code that tries to send headers AFTER this code?

Sub_Seven




msg:4252674
 2:09 am on Jan 13, 2011 (gmt 0)

Yes, I removed both echoes and they were the only two in the script.

Yes, this is the code the error is referring to, this is not an include, I just happen to have this script in my "includes folder" until I find a better place for it.

The script is being called from <form action="/includes/gc_insert.php" method="post">

and there are no included files within this script, could the header be carried from the page that has the form?

aakk9999




msg:4252679
 2:32 am on Jan 13, 2011 (gmt 0)

The way it reads to me is that your error says:

Headers are sent here (your code above).
So this headers command in the code must have worked then?
So perhaps the code AFTER this script also attempts to send headers, and the error says: sorry, cannot send them as I already send them in the code you quoted above.

Have you tried to remove ALL code and just send headers in the above script? Eg, have you tried:

<?php
header('Location: index.php');
?>

If this also gives you error, this probably means that the command above worked, and somewhere else you have a piece of code that tries to send headers again AFTER the above code but it cannot as they are already sent by the above command.

Sub_Seven




msg:4252692
 4:00 am on Jan 13, 2011 (gmt 0)

Ok, this:

<?php
header('Location: index.php');
?>

Causes the error to appear again, I don't understand what is sending those headers, this is so frustrating...

Anyango




msg:4252718
 8:11 am on Jan 13, 2011 (gmt 0)

is there any blank line before your <?php command ? its not just the php code, but any output in any form if it is sent you cannot then send headers. There must be some html output before your php thats causing this error
Look for any white line or a blank space if its not any actual html

aakk9999




msg:4252746
 10:52 am on Jan 13, 2011 (gmt 0)

Perhaps posting to one script and sending headers for another causes problem?

Why dont you post directly to index.php? E.g.
<form action="/index.php" method="post">

Then depending on POST variable you receive in index.php you execute your database insert and finish running script or execute normal code in index.php?

You could even send a hidden variable in POST as an indicator to index.php what it should be doing, then inspect that variable and run your code above if set, if not set, execute your normal index.php code.

For example, have hidden field gc_insert in your form then you could do something like:

if (array_key_exists('gc_insert', $_POST))
{
your DB insert code
}
else
{
your current index.php code
}

Matthew1980




msg:4252759
 11:11 am on Jan 13, 2011 (gmt 0)

my 10p:-

mysql_close() isn't required here, as the natural action of the query is to close upon the completion of the execution of the script.

Cheers,
MRb

Sub_Seven




msg:4252852
 2:56 pm on Jan 13, 2011 (gmt 0)

@Anyango

At the moment of testing, my file had nothing but those three lines and I did make sure there were no white spaces anywhere.

@aakk9999

Yesterday after I quit thinking about this and (temporarily) gave up I was ready to go to bed when something like your suggestion came to mind, I don't really need to redirect to my index (that was just for testing purposes), it needs to go more to a thank you page that I have not built yet, so that's what I'm about to try, I'll create this page and run the insert script right there and display the thank you message if insertion was successful :)

@Matthew1980

This is the second time you've told me that mysql_close() is not required in this type of connection, for some reason I keep doing it, OCD maybe? I don't know, I'll remove it and try to ignore the fact that it bothers me for some weird reason.

Thanks everybody :)

Anyango




msg:4253172
 6:30 am on Jan 14, 2011 (gmt 0)

Just for the sake of knowledge and discussion, Is there any chance you could be having a prepend going on here ? Maybe in php.ini or in your webserver settings or in .htaccess ?

If you didnt know, auto_prepend_file gives direction to php to prepend some code file before your code is executed. I dont think there is any such case here but its worth a look, because otherwise this header error is unexplainable. Look for that, maybe any other script file is being added to the top of your code, maybe by mistake.

for example in apache it could be like

php_value auto_prepend_file anyfile.php

omoutop




msg:4253188
 9:17 am on Jan 14, 2011 (gmt 0)

also try this:
i assume you build your page under windows with dreamwever.
open your page with notepad, in utf-8 encoding.
if you see a strange character on very top of page, delete it, upload page and see if the error appears again

Orangutang




msg:4253208
 9:58 am on Jan 14, 2011 (gmt 0)

I thought I'd post this just in case and because it may help someone else. I had problems with this error a while back whilst using header('Location: page.php'); . After searching on the net I found this which sorted all my errors out.

I put ob_start() at the very beginning of the script and ob_flush() at the very end.

I don't think I even need ob_start() at the beginning but I'd had enough of trying variations by the time I found these buffering functions.

Sub_Seven




msg:4253422
 5:58 pm on Jan 14, 2011 (gmt 0)

@omoutop

Windows= yes, Dreamweaver= no, I use notepad++ and this file is already utf-8, just in case I did open the file with notepad under utf-8 and there are no strange characters.

@Orangutang

I was hoping this would work but it didn't, unless I did it wrong, this is the way my file looked with your suggestion:

<?php
ob_start();
header("Location: /");
ob_flush();
?>

@Anyango

I guess we can rule out .htaccess since so far all I got in there is this to avoid getting an error page if extension name is left out:

Options +FollowSymLinks
RewriteEngine on
#
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

For the sake of testing purposes I deleted it and tried afterwards but the message was still there.

Now, I grabbed the same file, uploaded it to a whole different server from another different hosting company and there was the error again, so unless both companies have the same configuration my code has been damned by a greater force!

When trying to find out how to modify or access my php.ini all I got was that in order to do that I need to create a custom file and use .htaccess to accomplish what I want, I don't think it would be an easy approach plus that workaround is working already so I could say we run out of options...

Thanks to everyone who has helped :)

Anyango




msg:4253442
 6:19 pm on Jan 14, 2011 (gmt 0)

Don't want to waste your time but i just dont feel like giving up on this. I mean its programming, something either works or it doesnt, It isnt that it can not work without having a reason. We do need to find the reason why it isnt working. If nothing else it will make you a better programmer and teach you alot more about debugging and alot of patience :)

can you try the same code in another file on your same server ? just to see

For example

test.php

<?php
header("Location: http://www.example.com");
?>


and load that file directly, see what it says ?

Sub_Seven




msg:4253467
 6:44 pm on Jan 14, 2011 (gmt 0)

Oh you don't worry about my time, I'm the one who is requesting the help, if anything I'm the who worries about wasting your time, I can keep going for as long as your patience lasts, I've learned the hard way that if I wanna be on this industry patience is a must :)

I uploaded test.php and this is the error (same as before):

Warning: Cannot modify header information - headers already sent by (output started at /home/url/public_html/gotours/test.php:1) in /home/url/public_html/gotours/test.php on line 2

I'll send you the url in a message for you to see it for yourself, thanks.

Orangutang




msg:4253479
 7:16 pm on Jan 14, 2011 (gmt 0)

I've had a look through where I use it because the way you used it looks right to me.

<?php
ob_start();
header("Location: /");
ob_flush();
?>

I use it when I'm logging them in and checking their un and pw. I create different sessions for different users and redirect each to a different page using header('Location: page.php');. For what its worth I've posted my use below:

<?php
ob_start();
session_start();
require_once('fns_functions.php');

more code....select from db and loop through then I

if ($type == "superuser") {
$_SESSION['superuser'] = $username;
header("Location: pageone.php");
}
elseif($type == "manager") {
$_SESSION['mngruser'] = $clientusername;
$_SESSION['mngrname'] = $username;
header("Location: pagetwo.php");
}
elseif($type == "user") {
$_SESSION['stduser'] = $clientusername;
$_SESSION['stdusername'] = $username;
header("Location: pagethree.php");
}

mysql_close();
exit;
}
catch (Exception $e) {
do_html_header('Problem:');
?>

more code....login form etc

<?php
do_html_footer('');
mysql_close();
exit;
}
ob_flush();
?>

/////////////////////////////////////////////////

If I remove ob_flush() it still works but if I remove ob_start() I get this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\testsite\fns_db.php:15) in C:\xampp\htdocs\testsite\corpchkclientpass.php on line 35

Apparently the italics bit is where to look. I don't know exactly what's happening but one of the things causing me this error was my use of includes which I call at the top with require_once('fns_functions.php');.

Hopefully of some use?

topr8




msg:4253539
 10:10 pm on Jan 14, 2011 (gmt 0)

apache isn't prepending a file by any chance is it?

Sub_Seven




msg:4253549
 10:45 pm on Jan 14, 2011 (gmt 0)

@Orangutang

Based on your file and the way you are using code in italics it is the same I did, of course without all the extra code, so it should have worked if the cause of the problem was the same in both cases, according to the different results I would assume the the issue is not the same, good to keep this code in mind in case in the future I run into the same problem again and use it as another method for debbuging.

@topr8

I'm still a newbie when it comes to PHP, started my first project just a few months ago but I considered that I have learned tons in this period but when it comes to apache I gotta admit i'm still in diapers and I wouldn't have a clue were to check this, Anyango told me the same but I honestly don't know where to check if this is happening, I'm willing to check though if I'm given instructions.

Thanks so much :)

Sub_Seven




msg:4253555
 10:49 pm on Jan 14, 2011 (gmt 0)

Posted twice, sorry.

Anyango




msg:4254140
 11:04 am on Jan 17, 2011 (gmt 0)

Check your php.ini for this setting

auto_prepend_file =

is it just like this ? or there is any filename next to that = sign ?

topr8




msg:4254203
 3:00 pm on Jan 17, 2011 (gmt 0)

>>prepending

ah, sorry Anyango, i didn't spot that you'd mentioned this early on at the top!

Sub_Seven




msg:4254283
 6:08 pm on Jan 17, 2011 (gmt 0)

Oh man its gotta be something else, this is the way it looks that part of my php.ini:


; Automatically add files before or after any PHP document.
auto_prepend_file =
auto_append_file =

I don't think we had any other possible causes did we?

Sub_Seven




msg:4255020
 6:29 am on Jan 19, 2011 (gmt 0)

Hello everybody,

I'm glad to say this problem has been solved, I posted a ticket to my hosting company and this is the part that matters in their response:

"This error is occurring due to the file in question having a Byte Order Mark character at the beginning of the file. This character is interpreted by php as normal text and causes it to end the headers and begin output."

I changed the encoding of the file to UTF-8 without BOM and it worked right away, too bad I didn't try this earlier as encoding was mentioned several times in this thread...

Oh well, a lot of experienced has been gained though, thank you all for helping :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved