函数文档

is_ssl()

💡 云策文档标注

概述

is_ssl() 函数用于检测当前页面是否使用 SSL(HTTPS)。它通过检查 $_SERVER['HTTPS'] 或服务器端口 443 来判断,返回布尔值 true 或 false。

关键要点

  • 函数返回 true 如果页面使用 SSL,否则返回 false。
  • 检测机制基于 $_SERVER['HTTPS'] 是否为 'on' 或 '1',或 $_SERVER['SERVER_PORT'] 是否为 443。
  • 在某些负载均衡器或反向代理环境下(如 Network Solutions),is_ssl() 可能失效,需要额外配置。
  • 对于支持 HTTP_X_FORWARDED_PROTO 的环境,可通过修改 wp-config.php 文件来修复。
  • 函数自 WordPress 2.6.0 引入,4.6.0 版本从 functions.php 移至 load.php。

代码示例

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
    $_SERVER['HTTPS'] = 'on';

注意事项

  • 在负载均衡器后使用时,is_ssl() 可能无法正确检测 SSL,需根据环境调整配置。
  • 如果无法修改 $_SERVER 变量(如 Azure App Services),可能需要直接修改 is_ssl() 函数或使用替代函数。
  • 用户贡献笔记提供了针对 Cloudflare 和其他代理的扩展检测函数示例。

📄 原文内容

Determines if SSL is used.

Return

bool True if SSL, otherwise false.

More Information

Returns true if the page is using SSL (checks if HTTPS or on Port 443).

NB: this won’t work for websites behind some load balancers, especially Network Solutions hosted websites. To body up a fix, save this gist into the plugins folder and enable it. For details, read WordPress is_ssl() doesn’t work behind some load balancers.

Websites behind load balancers or reverse proxies that support HTTP_X_FORWARDED_PROTO can be fixed by adding the following code to the wp-config.php file, above the require_once call:

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS'] = 'on';

Source

function is_ssl() {
	if ( isset( $_SERVER['HTTPS'] ) ) {
		if ( 'on' === strtolower( $_SERVER['HTTPS'] ) ) {
			return true;
		}

		if ( '1' === (string) $_SERVER['HTTPS'] ) {
			return true;
		}
	} elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' === (string) $_SERVER['SERVER_PORT'] ) ) {
		return true;
	}

	return false;
}

Changelog

Version Description
4.6.0 Moved from functions.php to load.php.
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    With load balancers

    It won’t work for websites behind some load balancers, especially Network Solutions hosted websites. To bodgy up a fix, save this gist into the plugins folder and enable it. For details, read “WordPress is_ssl() doesn’t work behind some load balancers.”

    Websites behind load balancers or reverse proxies that support HTTP_X_FORWARDED_PROTO can be fixed by adding the following code to the wp-config.php file, above the require_once call:

    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
        $_SERVER['HTTPS'] = 'on';

  2. Skip to note 4 content

    if behind web proxy/balancer, can use this function.

    function wpdocs_maybe_is_ssl() {
        // cloudflare
        if ( ! empty( $_SERVER['HTTP_CF_VISITOR'] ) ) {
            $cfo = json_decode( $_SERVER['HTTP_CF_VISITOR'] );
            if ( isset( $cfo->scheme ) && 'https' === $cfo->scheme ) {
                return true;
            }
        }
    
        // other proxy
        if ( ! empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
            return true;
        }
    
        return function_exists( 'is_ssl' ) ? is_ssl() : false;
    }