PHP的setcookie函数,手册里是这么写的:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
大致意思是在setcookie之前不能有东西输出到客户端浏览器,否则会报错。但是经测试发现,并没有报错。继续翻看手册: