Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how to know if output already started?

Tags:

php

header

I use the same php script for including and for ajax loading. It is some random catalog items which are loaded with the page and can be loaded with ajax. I need to use header() function there only if it is loaded via ajax.

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

Thanks.

like image 876
foreline Avatar asked Jan 20 '11 21:01

foreline


2 Answers

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

// If no headers are sent, send one
if (!headers_sent()) {
    header('...');
    exit;
}
like image 55
Jake Avatar answered Sep 24 '22 13:09

Jake


There's an easy built-in for that:

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

Not much more to add :)

like image 26
tomwalsham Avatar answered Sep 22 '22 13:09

tomwalsham