What is the purpose of using the header() function in PHP to redirect to HTTPS?

When working with sensitive information or user data, it is important to ensure that the connection is secure. By using the `header()` function in PHP to redirect to HTTPS, you can ensure that the data being transmitted is encrypted and secure. This helps to protect against potential security threats such as man-in-the-middle attacks.

// Redirect to HTTPS if not already using it
if($_SERVER['HTTPS'] != 'on'){
    header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}