雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

PHP,JavaScript 获取当前域名、判断网址协议是否为 HTTPS

2024-09-07 47

1. PHP 判断当前协议是否为 HTTPS


/** * 判断当前协议是否为 HTTPS */function is_https(){    if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {        return true;    } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {        return true;    } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {        return true;    }    return false;}

2. PHP 获取包含网址协议的当前域名


echo domain();//https://www.itqaq.com
/** * 获取包含网址协议的域名 */function domain(){    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';    return $http_type . '://' . $_SERVER['HTTP_HOST'];}

3. JavaScript 判断当前协议是否为 HTTPS


function is_https(){    return 'https:' == location.protocol ? true: false;}

4. JavaScript 获取包含网址协议的当前域名


var domain = location.protocol + '//' + location.host
更新于:2个月前
赞一波!

文章评论

评论问答