What are common pitfalls when using header('Location:..') to redirect in PHP and how can they be avoided?
Common pitfalls when using header('Location:..') to redirect in PHP include not calling the function before any output is sent to the browser, not using an absolute URL for the location, and not exiting the script after the redirect. To avoid these issues, make sure to call header('Location:..') before any HTML or text output, use an absolute URL for the location, and exit the script after the redirect.
<?php
// Make sure to call header() before any output
header('Location: http://www.example.com/new_page.php');
exit; // Exit the script after the redirect
?>
Related Questions
- What best practices should be followed when passing multi-line text from an HTML form to imagettftext in PHP?
- How can prepared statements and bindParam be used in PHP to prevent SQL injection and improve database security?
- How can PHP error handling be implemented to troubleshoot issues related to session variable retrieval?