How can developers ensure that all form submissions are securely transmitted over HTTPS in PHP?

To ensure that all form submissions are securely transmitted over HTTPS in PHP, developers can enforce HTTPS by redirecting all HTTP requests to HTTPS using server-side code. This can be achieved by checking if the current request is not secure (HTTP) and then redirecting it to the secure version of the URL (HTTPS).

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