Are there any best practices or recommended methods for enforcing HTTPS protocol usage in PHP applications?

To enforce HTTPS protocol usage in PHP applications, one recommended method is to check if the request is not using HTTPS and then redirect the user to the HTTPS version of the site. This can help ensure that sensitive data is transmitted securely over the internet.

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