Programmatically determine if a connection is going through HTTP or through HTTPS?

I want to redirect users of my site from http://mysiteto https://mysite.

How can I do this programmatically in PHP?

+3
source share
4 answers

If $_SERVER['HTTPS']set to on, the page loads via https. However, you must mark your cookies as safe, which you can do with one of the options setcookie. If cookies are not protected, they can be transmitted via unencrypted http and can be stolen even if the user is redirected immediately.

http://php.net/manual/en/function.setcookie.php

.htaccess, PHP. .htaccess .

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond ^(.*)$ https://www.example.com/$1 [R=301,L]
+2

, SSL if(!empty($_SERVER['HTTPS'])).

, $_SERVER['SERVER_PORT'] == 443, , , HTTPS , , SSL .


PHP, .htaccess( Apache):

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
+7

Use $_SERVER['HTTPS']to define.

+2
source

If https, then the variable $_SERVER['HTTPS']will be set to a non-zero value:

if (isset($_SERVER['HTTPS'])) {
    # it https
} else {
    # it not https
}
+2
source

All Articles