PHP determine if using SSL HTTPS or HTTP

To determine if SSL is used, the is_ssl() function will return True if the page is using SSL (HTTPS or on Port 443), False if not used.

// From wordpress/wp-includes/load.php
// Previously wordpress/wp-includes/functions.php
function is_ssl() {
    if ( isset( $_SERVER['HTTPS'] ) ) {
        if ( 'on' == strtolower( $_SERVER['HTTPS'] ) ) {
            return true;
        }
        if ( '1' == $_SERVER['HTTPS'] ) {
            return true;
        }
    } elseif ( isset($_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
        return true;
    }
    return false;
}

4 comments

  1. anonymous

    thanks for posting this, it helped me big time today

  2. anonymous

    wtf it should be if ( $_SERVER['HTTPS'] = '1' etc..

    var you are checking = the value you want to check it against

Leave a Reply