What are the potential security risks of not enforcing HTTPS-only access for a PHP script?

Not enforcing HTTPS-only access for a PHP script can expose sensitive data to potential eavesdropping or man-in-the-middle attacks. To mitigate this risk, it is important to enforce HTTPS-only access to ensure that data is encrypted during transmission.

if ($_SERVER['HTTPS'] !== 'on') {
    header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}