Introduction: Why a Redirect in PHP Is Not Just header('Location:')
\n\nAnyone who has ever written in PHP has encountered the task of redirecting a user to another page. It seems simple, right? One line header('Location: /new-page'); — and you're done. But in practice, this simple mechanism turns into a headache for thousands of developers. It's no wonder the question “How do I make a redirect in PHP?” on Stack Overflow has millions of views.
The problem is that a redirect in PHP is not just a function call, but a whole process that is easy to break. Have you ever seen the error “Cannot modify header information”? Or noticed that the redirect works, but the page still loads? Or, even worse, encountered a situation where the redirect doesn't work at all, and the user sees a blank page?
\n\nIn this article, we will break down the 5 most common mistakes when creating a redirect in PHP, learn how to fix them, and find out how to make redirection safe and predictable. Let's go!
\n\nMistake #1: Redirect After Content Output (Header already sent)
\n\nThe Essence of the Problem
\nThe most common mistake beginners make is trying to send an HTTP Location header after HTML code or even a single space has already been sent to the browser. PHP throws a fatal error: “Cannot modify header information - headers already sent”.
Why Does This Happen?
\nThe HTTP protocol is designed so that headers must be sent strictly before the response body. As soon as PHP outputs something to the screen (via echo, print, or even an empty line before <?php), the headers are already fixed. They cannot be changed.
Example of the Mistake
\n<?php\necho "Hello!";\nheader('Location: /dashboard.php'); // ERROR: headers already sent\n?>\n\nHow to Fix It
\n- \n
- Rule #1: call
header()before any output, including spaces and HTML \n - Rule #2: make sure there are no spaces or empty lines in your PHP file before
<?phpor after?>\n - Rule #3: use output buffering if early output is unavoidable \n
<?php\n// Correct: redirect before any output\nheader('Location: /dashboard.php');\nexit;\n?>\n\nOutput Buffering (advanced method):
\n<?php\nob_start(); // Enable buffering\necho "Temporary content";\nheader('Location: /dashboard.php');\nob_end_flush(); // Send everything at once\nexit;\n?>\n\nMistake #2: Forgotten exit (or die) After Redirect
\n\nThe Essence of the Problem
\nCalling header('Location: ...') does not automatically stop script execution. PHP continues to process the code further, while the browser, having received the header, starts loading the new page. But what if resource-intensive code follows the redirect, or, even more dangerously, code that modifies data in the database?
Example of the Mistake
\n<?php\nheader('Location: /success.php');\n// User is already redirected, but PHP continues working!\nupdate_user_balance($userId, -1000); // DANGEROUS: deducting funds\nsend_email_to_admin("User made a purchase");\n?>\n\nHow to Fix It
\nAlways use exit (or die) immediately after calling header(). This ensures the script terminates and no extra code is executed.
<?php\nheader('Location: /success.php');\nexit; // Complete script stop\n// Code below will not be executed\nupdate_user_balance($userId, -1000);\n?>\n\nMistake #3: Incorrect URL (Relative vs Absolute)
\n\nThe Essence of the Problem
\nThe HTTP standard requires that the value of the Location header co