How 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:
- How to tell if it's an AJAX request in PHP (Monday, January 12th 2009)
- Image headers with PHP (Tuesday, July 22nd 2008)
- Using the HTTP_REFERER variable with PHP (Friday, April 18th 2008)
- 301 redirect with PHP (Saturday, March 15th 2008)
- PHP Magic Constants (Saturday, December 8th 2007)

Comments
blog comments powered by Disqus