PHP: how do I know if a start has started?

I use the same php script for including and for loading ajax . These are some random catalog items that are loaded by a page and can be loaded using ajax. I need to use the header() function there only if it loads via ajax.

When I use the header function and the result is already running, I get a warning about this php. How does php know that the output is already running? What is the best way to determine this, and not to call the header function?

Thanks.

+6
php header
source share
6 answers

http://php.net/manual/en/function.headers-sent.php

 // If no headers are sent, send one if (!headers_sent()) { header('...'); exit; } 
+9
source share

There is a simple built-in for this:

 if (!headers_sent()) { header('Your header here'); } 

Not much more to add :)

+4
source share

One option is to call ob_start() at the beginning, which buffers all your data . This way you can send headers at any time.

+2
source share

headers_sent() returns true if you cannot send additional headers.

+2
source share

If you sent anything to the client, the launch has begun.

For example, even one html tag, it is already output.

You can either structure your application to prevent this from happening, or you can enable output buffering:

http://php.net/manual/en/function.ob-start.php

0
source share

Just delete the empty line before running the code and add exit; after the conclusion is complete.

0
source share

All Articles