How to set a cookie

A

Anonymous

Guest
You can't set a cookie after you've sent output to the visitor's browser. By 'output', I mean anything, including print or echo statements, HTML tags, spaces, blank lines, text, anything.
 
You can even use javascript to set the cookies, using the following code instead of yours (like it is):
Code:
?><script>SetCookie('UserID',<?php echo $UserID; ?>, today.getTime()+31536000);</script><?php

Using something like this, you wont have that 'header' problems ;)
 
FraNL said:
In ASP it is no problem at all, hope it will also be possible in new versions of PHP.....

This is probably because ASP does something like output buffering by default. PHP also has this feature, but more wisely leaves it up to individual developers whether they want to make use of it. Generally it takes fewer resources and results in overall better code if you do without it.
 
Just a word of caution: There are a lot more users than you realize who have JavaScript turned off. Of course, there are a lot of users who have Cookies turned off, too.
 
swirlee is right!
Personally i never use cookies for that!
FraNL, you better use sessions... and cookies just as an 'ADDON' !!
 
gesf said:
FraNL, you better use sessions... and cookies just as an 'ADDON' !!

Um.. but sessions use cookies. Unless you're passing the SID via URL.
 
Well, a browser that has Cookies turned off will reject "Session cookies" in exactly the same way it rejects any other cookie. Just a word of caution.
 
Ohh damn! Of course!
Sorry, your right... i never thought about that! It makes sence!

Forgot everything i´ve said :p
 
Back
Top