Is there a way that I can set a cookie after an html output? According to PHP manual setcookie() should be set before the output.
I need it for my voting system wherein a cookie will be set after a successful Mysql query. I made it in one file.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Technically no. If you would like to set a cookie you need to ensure that no output has been send to the browser so far.
That’s correct, otherwise it won’t work. So I would even say must, not only should.
A successful mysql query on it’s own will not create any output. Only a failed mysql query would if error reporting is enabled. So I wonder if you actually ran into a concrete problem or not.
The mysql query itself should not prevent you from using
setcookie.In case you have done already HTML output prior the use of
setcookieyou need to find the place where your HTML output started. Above that line place theob_startDocs function which will start output buffering.With output buffering enabled, your program can still “output” HTML but it will not be send immediately. Then you should be able to call
setcookiewith no problems:The output buffer will be automatically send to the browser when your script finishes, so there is not much more you need to care about, the rest works automatically.