Page 1 of 1

Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:21 pm
by zenon
Good evening.

I create a php login with MySQL in Windows XP and when i shift it to an ubuntu server, when i'm trying to connect the browser shows me "Warning: Cannot modify header information - headers already sent".

I used session_register() and setcookie() and both show the same warning!

Is there any way of secure login without using session or cookies?

Thank you very much!


Best regards,
Zinon

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:23 pm
by Benjamin
:arrow: Moved to PHP - Code

The error message also details the file and line number where output is being sent. Should be an easy fix.

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:25 pm
by zenon
It shows me the line of the setcookie(), header() and session_register() in the php file.

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:29 pm
by Benjamin
Might as well post it ;)

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:31 pm
by zenon
I'm really sorry, but i got this warning on other's person computer and i don't have the exact string.
I looking for a solution in order to use it tomorrow :?

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:46 pm
by Benjamin
Sounds like your out of luck. How are you going to fix code you don't have access to?

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:50 pm
by zenon
Heh. I'm trying to find any possible solution to use it tommorrow on that computer, in order not to lose much time as today.
Any help is welcome :wink:

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 12:56 pm
by Benjamin
You cannot have any output before setcookie(), header() or session_register(). This means no html, no blank spaces, no php errors, nothing can be sent before you send data. Once you send data the headers are sent to the browser and cannot be modified.

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 1:00 pm
by zenon
astions wrote:You cannot have any output before setcookie(), header() or session_register(). This means no html, no blank spaces, no php errors, nothing can be sent before you send data. Once you send data the headers are sent to the browser and cannot be modified.
Thank you very much!

I have these before setcookie():

Code: Select all

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
        mysql_select_db("$db_name")or die("cannot select DB");
Does the die() output anything? When the connection is successful i think it's not output anything :?

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 1:07 pm
by Benjamin
Only if the connection fails, then it would. If you have blank space before your <?php tag, that would be considered output.

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 1:10 pm
by zenon
astions wrote:Only if the connection fails, then it would. If you have blank space before your <?php tag, that would be considered output.
Oh ok, thank you very very much.
Another one question please.
If i have a blank space after the ?> tag, does this consider as an output?
I mean i have something like:

Code: Select all

<?php 
?>
<html>
</html>
Or do i must do it like:

Code: Select all

<?php 
?><html>
</html>
Thank you again!

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 1:14 pm
by Benjamin
Both of your examples sent output.

You cannot do this:

Code: Select all

 
 <?php
# code here
?>
<html>
...
<?php
header("Location: foo");
 
It must be like this:

Code: Select all

<?php
header("Location: foo");
?>
<html>
...
 

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 1:20 pm
by zenon
Thank you very much from the bottom of my heart!
I appreciate your help!!

Re: Warning: Cannot modify header information - headers already

Posted: Wed Apr 29, 2009 6:30 pm
by kaisellgren
Simply put, the header() can be called only if you have not outputted anything. That is, echo(), print() and stuff outside PHP-tags can't exist before header() calls or you will get that ugly warning.

Re: Warning: Cannot modify header information - headers already

Posted: Thu Apr 30, 2009 11:19 am
by zenon
Good evening!!

Guys thank you very much from the bottom of my heart!
You are really helpfull!

astions once again I am under necessity to thank you! Finally, today my script worked thanks to you!

Have a nice day!


Sincerely yours,
Zinonas