PHP functions that send or modify HTTP headers must be executed before the requested page has been sent to the user. Otherwise, the following error will occur:
Warning: Cannot modify header information - headers already sent
(output started at file:line)
The PHP functions that modify the HTTP headers are the following:
header() / header_remove()
session_start() / session_regenerate_id()
setcookie() / setrawcookie()
And the ways to start sending content to the user before these functions are executed can be intentional or unintentional:
Intentional:
- Show information with print or echo
- Dump the content of variables with
var_dump()
- Use any of these functions:
printf(), trigger_error(), vprintf(), ob_flush(), readfile() o passthru()
.
- Add HTML code before the%% open% tag
Unintentional:
- Add some blank space before
<?php
or after <?php
- The BOM (Byte Order Mark) of UTF-8
- Error messages or notices produced previously