How can you handle cases where a page contains a redirection when measuring loading times in PHP?

When measuring loading times in PHP, you can handle cases where a page contains a redirection by following the redirection and then measuring the loading time of the final destination page. This can be done using PHP's cURL library to follow the redirection and calculate the loading time of the final page.

$url = "http://example.com/page-with-redirection";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$start = microtime(true);
$response = curl_exec($ch);
$end = microtime(true);
$loading_time = $end - $start;
echo "Loading time for page: " . $loading_time . " seconds";
curl_close($ch);