What headers or codes should be checked for to confirm a redirection in PHP?
To confirm a redirection in PHP, you should check for the HTTP headers such as "Location" which indicates the URL to which the browser should be redirected. Additionally, you can check for HTTP status codes like 301 (Moved Permanently) or 302 (Found) which indicate a redirection.
// Check for redirection using headers
if (isset($http_response_header)) {
foreach ($http_response_header as $header) {
if (strpos($header, 'Location:') !== false) {
$redirectUrl = trim(substr($header, 9));
echo "Redirected to: " . $redirectUrl;
}
}
}
// Check for redirection using HTTP status codes
if (http_response_code() == 301 || http_response_code() == 302) {
echo "Redirected with status code: " . http_response_code();
}