What is the recommended method in PHP to redirect to a new page after form submission?
After submitting a form in PHP, the recommended method to redirect to a new page is to use the header() function with the Location header set to the desired URL. This method sends a raw HTTP header to the browser, instructing it to redirect to the specified page. It is important to note that the header() function must be called before any actual output is sent to the browser to avoid errors.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process form data here
// Redirect to a new page after form submission
header("Location: newpage.php");
exit;
}
?>
Keywords
Related Questions
- What are the risks associated with using global variables like register_globals in PHP scripts and how can they be mitigated?
- What are best practices for handling errors related to string offsets in PHP arrays?
- What are the best practices for optimizing PHP code that involves querying databases to improve performance and reduce unnecessary traffic?