php判断网络协议请求类型是http还是https的方法
php判断网络协议请求类型是http还是https的方法如下:
<?php
function getRequestType(){
$type = 'http://';
if(((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))){
$type = 'https://';
}
return $type;
}
?>
条件太长不方便看可以这样:
<?php
function getRequestType(){
$type = 'http://';
$term1 = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') == true;
$term2 = (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') == true;
if($term1 || $term2){
$type = 'https://';
}
return $type;
}
?>
你还可以这样:
<?php
$httpType = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
?>
如果是解析已有的url连接,方法如下:
$httpType = strpos($url,'https://') !== false ? 'https://' : 'http://';