Detect SSL/HTTPS using PHP
The below code shows you how to detect if a website is using SSL/HTTPS
or not in PHP.
The code looks simple as this:
ssl-detection.php
<?php
function detectSSL()
{
// check HTTPS protocol
if (isset($_SERVER['HTTPS'])) {
if ('off' != $_SERVER['HTTPS']) {
return true;
}
if ('1' == $_SERVER['HTTPS']) {
return true;
}
}
// check server port
if (isset($_SERVER['SERVER_PORT'])) {
if ('443' == $_SERVER['SERVER_PORT']) {
return true;
}
}
// non-SSL
return false;
}
The above code, we simply check HTTPS
protocol and SERVER_PORT
in the $_SERVER
global variable.
Use the function:
<?php
if (detectSSL()) {
echo 'SSL';
} else {
echo 'NON-SSL';
}