Headers Already Sent
Warning: Cannot modify header information - headers already sent by (output started at index.php:5)Quick Answer
You are trying to send HTTP headers after output has already been sent to the browser. Move all header(), setcookie(), and session_start() calls before any echo, print, or HTML output.
Why This Happens
HTTP headers must be sent before any body content. Once PHP sends any output, including whitespace, echo statements, or HTML before the opening PHP tag, the headers are finalized. Any subsequent calls to header(), setcookie(), or session_start() will fail with this warning.
The Problem
<!DOCTYPE html>
<html>
<?php
session_start(); // Error: HTML was already output
header('Location: /dashboard');
?>The Fix
<?php
session_start();
header('Location: /dashboard');
exit;
?>
<!DOCTYPE html>
<html>Step-by-Step Fix
- 1
Find the premature output
The error message tells you exactly where output started. Check that file and line for echo, print, HTML, or whitespace before the opening <?php tag.
- 2
Move headers before output
Restructure your code so all header(), setcookie(), and session_start() calls happen before any output is sent.
- 3
Use output buffering if needed
If restructuring is difficult, add ob_start() at the very beginning of your script to buffer output, allowing headers to be sent later.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free