Warning: Cannot modify header information - headers already

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
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Warning: Cannot modify header information - headers already

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Warning: Cannot modify header information - headers already

Post 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.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post by zenon »

It shows me the line of the setcookie(), header() and session_register() in the php file.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Warning: Cannot modify header information - headers already

Post by Benjamin »

Might as well post it ;)
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post 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 :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Warning: Cannot modify header information - headers already

Post by Benjamin »

Sounds like your out of luck. How are you going to fix code you don't have access to?
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post 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:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Warning: Cannot modify header information - headers already

Post 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.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post 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 :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Warning: Cannot modify header information - headers already

Post by Benjamin »

Only if the connection fails, then it would. If you have blank space before your <?php tag, that would be considered output.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post 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!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Warning: Cannot modify header information - headers already

Post 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>
...
 
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post by zenon »

Thank you very much from the bottom of my heart!
I appreciate your help!!
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Warning: Cannot modify header information - headers already

Post 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.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: Warning: Cannot modify header information - headers already

Post 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
Post Reply