Saturday, April 4, 2015

Check if we are using HTTPS or HTTP - PHP Snippet

Check if we are using HTTPS or HTTP - PHP Snippet

Simple php function to check if we are using HTTPS or not.
/* 
*
* is_https function is used to check whether we are using HTTPS or not
* $_SERVER['HTTPS'] is the pivotal variable to scrutinize
* According to official documentation, $_SERVER['HTTPS'] is set to some non-empty value if HTTPS is used
* So, first we have checked whether $_SERVER['HTTPS'] is empty or not
* Special case: when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol
* So, we will check that $_SERVER['HTTPS'] is not set to 'off'
* Along with above mentioned two conditions, some ill-configured servers might not have $_SERVER['HTTPS'] defined
* even if SSL is used. So, we have checked for port too.
*
*/

function is_https(){
if ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)
return true;
else
return false;
}
Disqus Comments