What is the significance of checking the $_SERVER['HTTPS'] variable when implementing a redirect from HTTP to HTTPS in PHP?
The significance of checking the $_SERVER['HTTPS'] variable when implementing a redirect from HTTP to HTTPS in PHP is to ensure that the redirect is only performed if the current connection is not already secure. This prevents an infinite redirect loop that can occur if the redirect is triggered on every request, even those already using HTTPS.
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
$redirect_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('Location: ' . $redirect_url);
exit();
}