How to tell if it's an HTTPS request in PHPHow to tell if it's an HTTPS request in PHP

Posted January 26th, 2009 in PHP

This post looks at how to tell whether or not the request being made to the web server is an HTTPS request with PHP. This works for Apache - I'm not sure if the method used here works for other web server types.

If it's an HTTPS request the 'HTTPS' value in the superglobal $_SERVER array will be set and will be set to 'on'. If it is not an HTTPS request it will not be set.

So to test if it's an HTTPS request in PHP you could do this:

if( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) {
    ...
}

Alternatively you could set it as a constant if you need to know if it's an HTTPS request several times in your code like so:

define('IS_HTTPS_REQUEST', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');

Related posts:

Comments

blog comments powered by Disqus