Check first that $_SERVER['HTTPS'] isset

because some servers don't set $_SERVER['HTTPS'] if the connection is not secure