如何检查标头是否已在 PHP 中发送 [英] How to check if Headers already been sent in PHP

查看:27
本文介绍了如何检查标头是否已在 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 中发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆