How can PHP be used to switch between HTTP and HTTPS protocols without a redirect?

To switch between HTTP and HTTPS protocols without a redirect in PHP, you can check the `$_SERVER['HTTPS']` variable to determine if the current protocol is HTTPS. If it is not HTTPS, you can generate a new URL with the HTTPS protocol and the same request URI.

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