How to check if Headers already been sent in PHP

后端 未结 2 344
臣服心动
臣服心动 2021-01-03 18:00

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?

It would be really handy to

2条回答
  •  走了就别回头了
    2021-01-03 18:56

    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;
    }
    

提交回复
热议问题