I would like to do the following in php :
setcookie('name', $value, $Cookie_Expiration,'/');
then some action
header("location:http://www.example.com")
the problem is that I get :
warning: Cannot modify header information – headers already sent by (…etc )
could you please let me know what i am doing wrong and if there is a way to do this?
by the way , this code is before any output is made …the cookie setting part works fine on its own and so does the redirection code….the combination fails
thank you
Cookies are sent in the header, and you can’t set headers if any output is already sent to the browser (which is is when you set the cookie).
The easiest solution, mind you it is a bit sloppy is to use
ob_start()andob_clean(), for example:Please note the upper case L in the Location header, it is very important.
A better solution might be to set the cookie on the page you are redirecting to, and pass the information to set that header through a session.