views:

39

answers:

2
+3  A: 

Cookies are sent in the headers. All headers must be ready before the actual content (the title here) is sent.

That is, you should move the set cookie line before the <html> appears.

KennyTM
I moved it to the bottom of the file, but now error appears in 25 line, which is: </td></tr> <tr><td>"; if ($event == "") {
Tom
@Tom: What's your code looks like now?
KennyTM
I've fixed it with output buffering
Tom
+3  A: 

You cannot send HTTP headers after you have sent part of the content. (Cookies are set in the HTTP headers). Your three options are

  • Set the cookie before you send any output
  • Use output buffering to ensure no output gets sent before the cookie is set
  • Use JavaScript to set the cookie

The best option code structure wise is to set to cookie before you send any of the content. It is also good practice not to mix HTML and application logic, so moving your logic to before the HTML has two advantages.

Yacoby