Why is it important to ensure that a PHP application is only accessible via HTTPS and not HTTP?
It is important to ensure that a PHP application is only accessible via HTTPS and not HTTP to encrypt data transmitted between the server and the client, preventing unauthorized access to sensitive information. This helps protect user privacy, prevent man-in-the-middle attacks, and ensure the integrity of the data being exchanged.
// Redirect to HTTPS if accessed via HTTP
if($_SERVER['HTTPS'] != 'on'){
header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit();
}