What are some potential pitfalls to be aware of when using PHP scripts to check server status and perform automatic redirection?

One potential pitfall when using PHP scripts to check server status and perform automatic redirection is not properly handling error responses from the server. It is important to check for HTTP status codes that indicate an error (such as 4xx or 5xx codes) and handle them appropriately, either by displaying an error message or redirecting to a different page.

// Check server status and perform automatic redirection with error handling

$url = 'http://example.com/check-status';
$response = file_get_contents($url);

if ($response === false) {
    // Handle connection error
    echo "Error connecting to server";
} else {
    $httpCode = explode(' ', $http_response_header[0])[1];
    
    if ($httpCode >= 400 && $httpCode < 600) {
        // Handle server error
        echo "Server error: " . $httpCode;
    } else {
        // Perform redirection
        header("Location: http://example.com/redirected-page");
        exit();
    }
}