setcookie issues

writing a simple user system using cookies for persistent sessions. anyways, ive hit a snag with the php installed on my hosting(1and1).

i was testing the script on my local webserver, and it worked great, it set the cookie just fine. then entire script was running smooth. well i upped it to my site and i found it was not setting the cookie. after about 3 hours of playing with it, ive learned that its not sending the set-cookie: header at all, BUT if i move the setcookie() to something before the first of the html tags(like before <html>) it will set the cookie fine. from what i can see, php is sending out the headers and info before it even parses the setcookie() line, which means its already too late to set the cookie since the headers have already gone out.

all i can guess is that this is some form of a bug in the older php on the webhosting i am using. php 4.4.4 is what is running on the webhosting. im running php 5.0.4 on my local server.

my question is, is this really a bug or some form of older php crud. and is there an easy way around this, since putting the setcookie() before all html tags would be a monumental pain in the butt, being there are about 20 echo's in various if's well before the setcookie() line.
[1280 byte] By [mirthen] at [2007-11-20 7:29:18]
# 1 Re: setcookie issues
I'm using Oneandone mirthen so I can help you with the php version issue. Just upload the following to an .htaccess file in the root of your website.

AddType x-mapp-php5 .php

If you don't know about htaccess then what you need to do is:

1) Open notepad and paste in said code.
2) Save as a.htaccess (it shouldn't have a filename but notepad need at least one char).
3) Upload to web server
4) Remove the a from the filename so it just says .htaccess
5) You are now running PHP 5!

Regarding set cookies. I think you need to use them before any output. When I did mine they just looked like this:

setcookie("code",$aaa,time()+15768000);
setcookie("user",$bbb,time()+15768000);

Consider using output buffering to resolve your problem with having to set cookies first, it means you can do things in pretty much any order.
Nibinaear at 2007-11-10 3:56:32 >
# 2 Re: setcookie issues
I think you need to use them before any output.
That is the key phrase.
PeejAvery at 2007-11-10 3:57:30 >