PHP获取网站URL协议 – http vs https
我写了一个小函数来build立当前的网站url协议,但我没有SSL,不知道如何testing它是否在https下工作。 你能告诉我,如果这是正确的?
function siteURL() { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $domainName = $_SERVER['HTTP_HOST'].'/'; return $protocol.$domainName; } define( 'SITE_URL', siteURL() ); 是否有必要像上面这样做,或者我可以这样做吗?
 function siteURL() { $protocol = 'http://'; $domainName = $_SERVER['HTTP_HOST'].'/' return $protocol.$domainName; } define( 'SITE_URL', siteURL() ); 
在SSL下,即使定位标记url使用http,服务器是否也会自动将url转换为https? 是否有必要检查协议?
谢谢!
这不是自动的。 您的顶级function看起来不错。
我知道这是晚了,虽然有一个更方便的方法来解决这种问题! 上面显示的解决scheme是相当混乱的,如果有人应该再次检查这一点,我会怎么做:
 $protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://'; 
甚至没有条件,如果你不喜欢
 $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,strpos( $_SERVER["SERVER_PROTOCOL"],'/'))).'://'; 
  看看$_SERVER["SERVER_PROTOCOL"] 
这对我有用
 if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $protocol = 'https://'; } else { $protocol = 'http://'; } 
一些变化:
 function siteURL() { $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $domainName = $_SERVER['HTTP_HOST']; return $protocol.$domainName; } 
简短的方式
 $scheme = $_SERVER['REQUEST_SCHEME'] . '://'; 
对于除IIS以外的任何系统,这足以定义站点自我URL:
 $siteURL='http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].'/'; 
要么
 $siteURL='http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].'/'; 
取决于你到底想要什么: HTTP_HOST与SERVER_NAME
由于根据我的testing端口号不是一个好的做法,我的解决scheme是:
 define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)); 
 如果设置了$_SERVER['HTTPS']并且等于“1”,“true”,“on”或“yes”, HTTPS常量返回TRUE 。 否则返回FALSE。 
使用此服务器variables获取协议详细信息:
  $scheme = $_SERVER['REQUEST_SCHEME'] . '://'; echo $scheme; //it gives http:// or https:// 
请注意,这个服务器variables是不可靠的。 有关更多信息,请看看: $ _SERVER ['REQUEST_SCHEME']是否可靠?
 $protocal = 'http'; if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || $_SERVER['HTTPS'] == 'on') {$protocal = 'https';} echo $protocal; 
 在代理的情况下, SERVER_PORT可能不会给出正确的值,所以这是我的工作 – 
 $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTP_X_FORWARDED_PORT'] == 443) ? "https://" : "http://" 
用Rid Iculous的答案在我的系统上做了一个函数。
 function site_protocol() { if(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') return $protocol = 'https://'; else return $protocol = 'http://'; } 
希望它有帮助
这是https或http的最佳解决scheme:
 <?php $protocol = '//'; $site_url = $protocol.$_SERVER["HTTP_HOST"]; ?> 
但不能显示HTTPS或HTTP,所以它只能用来链接你的网站内容,如图像等
如果要在https中redirect您的网站,请在.htaccess文件中添加以下代码:
 <IfModule mod_rewrite.c> RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' RewriteRule ^(.*)$ https://www.your-domain.com$1 [L] </IfModule> 
用你的昵称更改http://www.your-domain.com 。
 这是我怎么做的…这是一个速记, if else版本的Rid Iculous的答案… 
 $protocol = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http'; 
我已经testing了最多的答案 , 并没有为我工作 ,我最终使用:
 $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';