If, for some reason, you absolutely have to send part of your HTML output before setting the cookie, you can put some javascript later in the page that sets a cookie on the fly.
This may come with its own set of problems, though, so rearranging things to put all of your output at the end probably is a better solution.