What is the common error message encountered when trying to use header(Location) after starting a session in PHP?
When trying to use `header(Location)` after starting a session in PHP, the common error message encountered is "Cannot modify header information - headers already sent". This error occurs because headers must be sent before any output is sent to the browser, and starting a session sends headers. To solve this issue, you can use output buffering to buffer the output before starting the session.
<?php
ob_start(); // Start output buffering
session_start(); // Start the session
// Your code here
header("Location: example.php"); // Redirect to example.php
ob_end_flush(); // Flush the output buffer
?>
Keywords
Related Questions
- What are potential pitfalls when trying to display the newest post first in a PHP script?
- How can PHP developers ensure accurate date/time calculations when working with Unix timestamps in their applications?
- What are the potential pitfalls of confusing HTML and PHP when working with page names in PHP scripts?