How can you access and evaluate the header of a redirect in PHP?
When a redirect occurs in PHP, you may need to access and evaluate the headers of the redirect response for various reasons, such as checking the status code or extracting specific header values. To do this, you can use the PHP function get_headers() to retrieve an array of headers from the specified URL. You can then loop through the array to access and evaluate the headers as needed.
// Specify the URL of the redirect
$url = 'http://example.com';
// Get the headers of the redirect URL
$headers = get_headers($url, 1);
// Loop through the headers array to access and evaluate the headers
foreach($headers as $key => $value) {
echo $key . ': ' . $value . '<br>';
}
Keywords
Related Questions
- What steps can be taken to ensure that the correct character encoding (ISO-8859-1 or UTF-8) is used in PHP forum output?
- How can file operations be used to store and compare the state of a URL in PHP?
- What are alternative functions in PHP that can be used for adding and removing slashes more securely?