You can't send/set a cookie after the headers. You should really investigate Mojolicious, Dancer2, Catalyst (worst choice for CGI deployment), et cetera. They all handle huge amounts of this stuff automatically. And take some time to read the links and tutorials that other monks have suggested. If you just hammer away at a problem until an answer gives, you're not learning anything, the problems will persist, and you'll slowly alienate anyone who was willing to help when they see they're just helping with the same class of question over and over.