How to check if Headers already been sent in PHP(如何检查标头是否已在 PHP 中发送)
问题描述
我想我们大多数人都知道 PHP 中臭名昭著的标头已发送"错误.我可以检查一下是否已经发送了标头吗?
I think most of us know about the infamous "Headers already sent" error in PHP. Can I check someway if headers already have been sent?
在全力以赴尝试设置一些 SESSION 数据或类似数据之前,这样做真的很方便.
It would be really handy to do such, before going all out with trying to set some SESSION data or similar.
谢谢!
推荐答案
PHP有一个函数 headers_sent()
允许您在采取任何操作之前检查标头是否已发送出去.以下是在代码中使用该函数的方法:
PHP has a function headers_sent()
which allows you to check if the headers are already sent out before you take any action. Here’s how you could use the function in your code:
if(headers_sent())
{ //if headers already sent out print some message.
echo "Please go to yahoo.com";
}
else{
//send the user automatically to test.php
header('Location: http://yahoo.com');
exit;
}
这篇关于如何检查标头是否已在 PHP 中发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查标头是否已在 PHP 中发送


- PHP Count 布尔数组中真值的数量 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Laravel 仓库 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01