What is the correct syntax for using header(Location: URL) in PHP to redirect to a specific URL?
When using the header() function in PHP to redirect to a specific URL, the correct syntax is to use the Location header followed by the URL you want to redirect to. It is important to note that there should not be any output sent to the browser before using the header() function, as headers must be sent before any content. Additionally, the URL should be an absolute URL including the protocol (http:// or https://).
<?php
// Redirect to a specific URL
header("Location: https://www.example.com");
exit;
?>
Keywords
Related Questions
- Is it possible to clear form fields by reloading the page with original parameters in PHP?
- What best practice is recommended in the forum thread to ensure emails are only sent under specific conditions?
- What are some best practices for ensuring smooth transition to newer PHP versions, particularly when dealing with legacy scripts and applications?