Forum Moderators: coopster

Message Too Old, No Replies

setcookie and session_start

         

Gruessle

8:40 pm on Apr 24, 2005 (gmt 0)

10+ Year Member




I can't do setcookie after seesion_start.
I been hours on this now and can't figure it out.

In CODE words
==============================
// This works:
setcookie("test", "test", time()+3600);
session_start();

==============================
// This does NOT work:
session_start();
setcookie("test", "test", time()+3600);

WHY?

mcibor

9:10 pm on Apr 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



setcookie works only before sending headers. session_start sends a cookie of it's own. However I myself don't understand the problem of php in sending cookies.

I just know, that there isn't any html allowed before, nor any function.

So these two won't work:

function my()
{
return true;
}
my();
setcookie(...);

nor
echo "";
setcookie(...);

If the code setcookie(..);session_start(); works for you, then use it.
Best regards
Michal Cibor

PS There's a possibility to set a cookie after headers sent -use javascript onload.

ergophobe

9:54 pm on Apr 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




function my()
{
return true;
}
my();
setcookie(...);

It's just output that affects sending headers, not function calls (and cookies are sent along with the other headers). Calling a function that has no output does not prevent you from sending headers or setting cookies.

dmmh

6:38 am on Apr 25, 2005 (gmt 0)

10+ Year Member



this is why we have the wonderful function ob_start() :)