How can the header() function in PHP be utilized to redirect users to a new URL?
To redirect users to a new URL in PHP, you can use the header() function with the 'Location' parameter set to the desired URL. This function sends a raw HTTP header to the browser, which then redirects the user to the specified location. Make sure to call the header() function before any output is sent to the browser to avoid any errors.
<?php
// Redirect to a new URL
header("Location: http://www.example.com");
exit; // Make sure to stop the script execution after the redirect
?>
Keywords
Related Questions
- How can I effectively work with arrays in PHP when dealing with JSON data?
- What is the best approach to monitoring an FTP directory for new files and sending an email notification using PHP?
- In the context of PHP development, what are some common mistakes that developers make when organizing class files and handling inheritance relationships?