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

查看:83
本文介绍了如何检查标头是否已经在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具有功能

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天全站免登陆