How can the "Cannot modify header information - headers already sent" error be resolved in PHP?
The "Cannot modify header information - headers already sent" error occurs in PHP when there is output sent to the browser before header functions are called. To resolve this issue, make sure there is no whitespace or output before calling header functions like header(), setcookie(), or session_start(). You can also use output buffering to capture any output before sending headers.
<?php
ob_start(); // Start output buffering
// Your PHP code here
ob_end_flush(); // Flush the output buffer
Related Questions
- What is the purpose of mod_rewrite in PHP and how can it be used to rewrite URLs?
- How can UNION be used in PHP to combine multiple SQL queries into a single result set?
- How can the issue of being able to access a protected page by clicking the back button in the browser be prevented in a PHP script?