Problem with using cookies: "Warning: Cannot modify header"

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
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Problem with using cookies: "Warning: Cannot modify header"

Post by MicroBoy »

It doesn't metter in which page I use cookies every time it says:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\elektro\pjeset\index2.php:1) in C:\xampp\htdocs\elektro\pjeset\index2.php on line 2
This is my test page:

Code: Select all

<?php
setcookie("kodi", "kossss", time()+3600);
?>
<html>
<head>
<title>Test Page</title>
</head>
<body>
Test Text
</body>
</html>
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Problem with using cookies: "Warning: Cannot modify header"

Post by Mark Baker »

Is the script saved as UTF-8?
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Problem with using cookies: "Warning: Cannot modify header"

Post by MicroBoy »

Mark Baker wrote:Is the script saved as UTF-8?
Thanks a lot :D Everything it's ok, since I converted to UTF-8. :D
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Problem with using cookies: "Warning: Cannot modify header"

Post by Mark Baker »

MicroBoy wrote:
Mark Baker wrote:Is the script saved as UTF-8?
Thanks a lot :D Everything it's ok, since I converted to UTF-8. :D
Major rule. Never save the actual script files as UTF-8. Learned that one myself the hard way
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: Problem with using cookies: "Warning: Cannot modify header"

Post by JasonDFR »

I save all of my scripts as UTF-8 and have never had a problem. Should I be worried? Under what conditions can there be problems?

EDIT: I do not write the unicode BOM however.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Problem with using cookies: "Warning: Cannot modify header"

Post by Mark Baker »

JasonDFR wrote:I save all of my scripts as UTF-8 and have never had a problem. Should I be worried? Under what conditions can there be problems?

EDIT: I do not write the unicode BOM however.
I don't know if it's different for different platforms/web servers/editors.

Certainly with the editors that I use, if I save a file as UTF-8, then it automatically writes a BOM header to that file, which is totally transparent unless I view the file in a binary editor.
However, when the web server processes that file, it may see the BOM header as output and send it to the browser. Becaue it's the first thing in any file, then it gets sent before any headers in the script... and the web server sends its default headers, then the BOM marker as output.

I prefer not to take the risk, and have no need to do so because I don't embed anything that requires UTF-8 within the scripts themselves.
I've seen too much of other people having problems to want to fall into the same trap myself
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Problem with using cookies: "Warning: Cannot modify header"

Post by php_east »

Mark Baker wrote:
JasonDFR wrote:I save all of my scripts as UTF-8 and have never had a problem. Should I be worried? Under what conditions can there be problems?

EDIT: I do not write the unicode BOM however.
I don't know if it's different for different platforms/web servers/editors.

Certainly with the editors that I use, if I save a file as UTF-8, then it automatically writes a BOM header to that file, which is totally transparent unless I view the file in a binary editor.
i have had to use utf-8 for some include files, the specifications calls for such. i think the utf-8 files are ok if they do not involve headers ( i.e. ok for include files and such ). it is ony when there is output to be done that the byte order marks come into play. i am not able to confirm this however, only my suspicion. i tend to use an editor which has on option setting for byte order mark use.
Post Reply