Setting cookies and writing to a db



I have a little problem in that I'd like to save some information to a database and also save some info to the user's h/d in a cookie. The problem is that I can't save cookies after headers have been sent, and after the db has been written, I'd like to show a thankyou message, so I need to send headers sometime before this.

save to cookie
show header
write to db

But after I write to the cookie, the processing on that php file will just stop - is there anything I can do here?
Yeah, that would have made things a lot clearer, wouldn't it. However, my initial premise that I couldn't do anything after setting a cookie was false - I can do whatever I like as long as the cookie is set before any headers are sent.

So, no problem. Apologies for wasting people's time (esp. liquedus)