Appearance
PHP: HTTP or HTTPS?
In PHP, you can use the `$_SERVER['HTTPS']` variable to check whether the current page is being served over HTTPS. If the variable is set and has a value of on, the page is being served over HTTPS.
You can also use the `$_SERVER['SERVER_PORT']` variable to check the port number being used for the current connection. Port 443 is the default port for HTTPS connections, so if the value of this variable is 443, the page is likely being served over HTTPS.
With PHP, you can also redirect users to the HTTPS version of your website by using a simple code snippet at the beginning of your script.
Example of redirecting users to HTTPS in PHP
php
<?php
if (empty($_SERVER['HTTPS']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] !== 'https' && $_SERVER['SERVER_PORT'] != 443) {
$url = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header("Location: $url");
exit();
}This will redirect all incoming requests to the HTTPS version of the website.
It is important to note that if you are using a load balancer or a reverse proxy, it may handle SSL termination. In that case, you will need to check the headers set by the load balancer or proxy to identify whether the request is coming in over HTTPS.