PHP setcookie()之前不能有任何输出

本文探讨了PHP中setcookie()函数的使用限制,必须在任何输出之前调用以符合HTTP协议。虽然手册提到有报错风险,但通过测试发现,启用输出缓冲区(ob_start()和ob_end_flush())可以避免错误。当output_buffering配置为0时,需要手动管理缓冲区。PHP 5.3及更高版本默认开启缓冲区,因此在setcookie前的输出会被包含在HTTP响应中,避免报错。
摘要由CSDN通过智能技术生成

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之前不能有东西输出到客户端浏览器,否则会报错。但是经测试发现,并没有报错。继续翻看手册:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值