PHP warning when trying to set a cookie

edg3

Well-Known Member
Joined
Jan 10, 2005
Messages
187
Reaction score
0
Location
Randpark Ridge
Hi there,

I have been playing around in php for a few weeks now on my local web server and decided it would be a great idea to write a php post management system for myself (previously I was using Drupal). It works perfectly fine on my webserver here at home, but once uploaded it work, but I cant set cookies. It gives me the error:

Code:
Warning: Cannot modify header information - headers already sent by (output started at /usr/www/users/realdq/dologin.php:1) in /usr/www/users/realdq/dologin.php on line 6

Source (Without any linking to login databases till I can set the cookie)

After reading around I saw that its more commonly called a "whitespace" error, although I have checked a few times and cant find any place where there is an extra space that could cause it. A friend of mine suggested that it could be to do with not running session_start() on one of my pages, so I tried adding it onto my index like he suggested, and it gives this error:
Code:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /usr/www/users/realdq/index.php:1) in /usr/www/users/realdq/index.php on line 1

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /usr/www/users/realdq/index.php:1) in /usr/www/users/realdq/index.php on line 1

I also read that there was an issue when using session related variables and it overwriting cookies on hetzner which was posted in offtopic, but even with the change of the variable "register_globals" to 0 in a htaccess file there is no change.

Any help would be greatly appreciated.
 
Is your development machine a windows box and the deployment server a linux box?

The warning is telling you were the problem lies "(output started at /usr/www/users/realdq/dologin.php:1)"

The most likely reason is that your php file has windows line-breaks and spacing rules. Try running it through the dos2unix util.
 
Is your development machine a windows box and the deployment server a linux box?

The warning is telling you were the problem lies "(output started at /usr/www/users/realdq/dologin.php:1)"

The most likely reason is that your php file has windows line-breaks and spacing rules. Try running it through the dos2unix util.
Ok, I dont have access to dos2unix at the moment (unless there is a windows version somewhere) and the deployment box is linux as far as I know.

Will search around to try get it converted from current format to unix line-breaks and such then.
 
There is this site: http://www.dos2unix.org/

Never used it so can't vouch for its utility but it might be worth a try?
Doesnt seem to be working unfortunately, all it does is allow me to browse to the file and then doesnt even use it. It pulls down a .pl file called Dos2Unix. I could use that though I guess, is it a Perl file?

EDIT: there was a link to a program called "tofromdos" which supposedly converts it between the two formats. That hasnt worked either.
 
Last edited:
Install Scite on windows. It have a menu option to save using unix or dos linebreaks.
 
Install Scite on windows. It have a menu option to save using unix or dos linebreaks.
I used scite and saved all the files with unix line breaks but I still have the error.

malec said:
you could also try to use output buffering. see ob_start()
I looked at output buffering and I still cant get it to work, I simplified the whole page down to the following to try get rid of any extra spaces or line breaks:

PHP:
<? ob_start(); $uname = $_POST['uname']; $pass = $_POST['pass']; if ($uname == "xxxxxx") { if ($pass == "xxxxxx") { setcookie('code', '1234', time() + 600); } } ob_end_flush(); ?>
This still gives me the error.

Reading a bit more on output buffering I found that some people needed to change their php settings, so here is my .htaccess file:
Code:
php_value register_globals 0
PHP_FLAG output_buffering on

Any other suggestions?
 
Last edited:
Make sure that the first line starts with <?php
Remove all blank lines and spaces before <?php
 
Try this.
It will prevent any undefined index errors if the form was not submitted:

PHP:
    if (isset($_POST['uname']) && isset($_POST['pass']) && $_POST['uname'] == "xxxxxxx" && $_POST['pass'] == "yyyyy")
    {
        setcookie('code', '1234', time() + 600);
    }
 
Try this.
It will prevent any undefined index errors if the form was not submitted:

PHP:
    if (isset($_POST['uname']) && isset($_POST['pass']) && $_POST['uname'] == "xxxxxxx" && $_POST['pass'] == "yyyyy")
    {
        setcookie('code', '1234', time() + 600);
    }
Thats just the thing:
1) The code works perfectly fine on my local web server
2) The values that Im using are being sent, that isnt the problem
3) The issue is with the set_cookie() command regardless of whether the other variables are set. If I had set_cookie() on its own in the php file it would still give the error.
The problem isnt the if statements, even if I can improve them.
 
hmm
My next guess would then be permissions. Does the webserver's process have write access to the session directory?
 
Ok, so thanks to some help from a friend (potato) I have managed to get my errors sorted.

You need to have ob_start() and ob_end_flush() around your header/cookie code, and if you are using a windows based editor (eg. Windows Web Developer 2008) you should save your php file using a different editor (eg. Notepad).

This for some reason removes the whitespace errors that I was getting when just trying to set the cookie or when just using ob_start() and ob_end_flush() to try fix the problem.

Im hoping someone could suggest a reason for this? One would think all Microsoft products would be standardised and as such wouldnt have different handling of line-breaks and such, or is notepad perhaps just old and therefore uses an older version?
 
Cannot modify header information - headers already sent

that usually means you're trying to modify something in the headers that has been set already. So make sure that code runs right at the top of your page.

I'm not sure why WWD 2008 would do that, I use Notepad wherever I can for everything.
 
Top
Sign up to the MyBroadband newsletter
X