I’ve been working on a REST API that sends header information as well as a corresponding web site that sets cookies. I’d tried to debug some of the PHP scripts by printing out text as the files are interpreted. That seems to work well except I was always getting a 200 OK for the REST calls even though I was setting the response code to something else, and I for the web page I wasn’t able to use the PHP built-in function setcookie properly. After a little digging around I discovered that I can’t output anything prior to calling header functions like header and http_response_code and setcookie.

Just remember that you need to remove your echo calls before calling header and setcookie functions if you want them to succeed.