Quick Answer
Redirect users to different pages.
Understanding the Issue
PHP provides several methods to redirect users while ensuring no output is sent before headers.
The Problem
This code demonstrates the issue:
Php
Error
echo "Content";
header("Location: newpage.php"); // Fails
The Solution
Here's the corrected code:
Php
Fixed
// Proper redirection
header("Location: newpage.php");
exit;
// Output buffering
ob_start();
// ... content ...
ob_end_clean();
header("Location: newpage.php");
// HTTP status codes
http_response_code(301);
header("Location: permanent/newpage.php");
// JavaScript fallback
echo '<script>window.location = "newpage.php";</script>';
Key Takeaways
Always call header() before any output and terminate with exit.