Can't I set session and cookie in same PHP file?
I get an error message if I set the cookie after I've set session telling me that the header is already sent.
If I set session after cookie I get nothing but it seems not to work well.
Can't I set session and cookie in same PHP file?
I get an error message if I set the cookie after I've set session telling me that the header is already sent.
If I set session after cookie I get nothing but it seems not to work well.
The short answer is yes - you can set SESSION and COOKIE data in the same PHP file.
The longer answer:
It is likely that in your case, you have sent the header and/or started outputting data to the client in the same place you are setting SESSION data.
See the PHP manual: Cookies for more details. In particular the quote of:
"Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser. This is the same limitation that header() has. You can use the output buffering functions to delay the script output until you have decided whether or not to set any cookies or send any headers."
If you need further help - try inserting your sample code/page that you are having issues with.