In the context of PHP, how can header() function be utilized to redirect to a custom error page?
To redirect to a custom error page using the header() function in PHP, you can set the HTTP header status code to 301 or 302 for a temporary or permanent redirect, respectively. You can then specify the location of the custom error page in the header using the Location parameter.
<?php
// Set the HTTP status code to 301 for a temporary redirect
header("HTTP/1.1 301 Moved Permanently");
// Redirect to the custom error page
header("Location: custom_error_page.php");
exit();
?>
Related Questions
- What are the best practices for debugging PHP scripts that involve database queries, such as using var_dump() and other debugging techniques?
- How important is it for developers to continuously update their PHP knowledge and skills, especially with new versions and deprecated functions?
- What are the potential challenges of transferring data between tables in PHP?