Check if PHP is using HTTPS (SSL)

Posted on December 9th, 2011 by Wayne May in PHP | 0 comments

I had to come up with a way to check if PHP is running in SSL mode today. This will work on both Apache, Nginx and IIS

1
2
3
4
5
6
7
8
9
10
11
12
function is_ssl()
{
    $secure_connection = false;
 
    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
        || $_SERVER['SERVER_PORT'] == 443) 
    {
        $secure_connection = true;
    }
 
    return $secure_connection;
}

I found this little nifty function on StackOverflow

Post a comment

Hello guest, care to post a comment?