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 中发送
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
