Why is it important to use absolute URLs in header() redirects in PHP scripts?

Using absolute URLs in header() redirects in PHP scripts is important because it ensures that the redirect works correctly regardless of the current page's location or the server configuration. Relative URLs may cause issues if the script is accessed from different directories or domains. Absolute URLs provide a complete path to the destination, making the redirect more reliable and consistent.

// Incorrect redirect using relative URL
// header('Location: /new-page.php');

// Correct redirect using absolute URL
header('Location: https://example.com/new-page.php');
exit();