https 检测 - 这两种方法的优缺点
我希望对我在页面中链接的资源进行 https/http 的“智能”检测。基本上,如果页面需要有效的 ssl,我只能放置 https
href。
选项 1(使用 PHP)
<?php
if ( $_SERVER["SERVER_PORT"] == "80" ) {
$http = "http";
} else {
$http = "https";
}
?>
<script type="text/javascript" src="<?=$http?>://myUri.com/script.js"></script>
选项 2(使用 //
而不是 http://< /code> 或
https://
)
<script type="text/javascript" src="//myUri.com/script.js"></script>
使用选项 3 https
对于一切
我有什么理由不应该只使用 https 来做所有事情吗?
I would like to have 'intelligent' detection of https/http for the resources I link within my pages. Basically if the page needs a valid ssl, I can only put https
href's.
option 1 (using PHP)
<?php
if ( $_SERVER["SERVER_PORT"] == "80" ) {
$http = "http";
} else {
$http = "https";
}
?>
<script type="text/javascript" src="<?=$http?>://myUri.com/script.js"></script>
Option 2 (using //
instead of http://
OR https://
)
<script type="text/javascript" src="//myUri.com/script.js"></script>
Option 3 use https
for everything
Is there any reason I shouldn't just use https for everything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与协议相关的
//
是处理此问题的最直观方法。然而,我不确定旧版浏览器是否普遍支持它。但在 PHP 中,您不应该检查
$_SERVER["SERVER_PORT"] == "80"
,因为您可能在某些时候需要在 80/443 以外的端口上运行 Web 服务器,例如在开发中。相反,请检查HTTPS
服务器变量的内容:The protocol-relative
//
is the most intuitive way of handling this. I am not certain it is universally supported by older browsers, however.In PHP though, you should not check for
$_SERVER["SERVER_PORT"] == "80"
, as you may at some point need to run your web server on a port other than 80/443, in development for example. Instead, check the contents of theHTTPS
server variable:选项 1:这并不可靠。不太可能有人会通过端口 80 执行 SSL,但这是可能的。
选项 2:这是强大、简单且安全的。使用它。
选项 3:如果您对所有脚本使用 SSL,但不对页面使用 SSL,那么您将收到混合内容警报。通过 SSL 提供所有内容将对缓存和性能产生影响(但对于需要用户登录的网站的任何部分来说通常是一个好主意)。
Option 1: This isn't robust. It is unlikely that someone will do SSL over port 80, but it is possible.
Option 2: This is robust, simple and safe. Use it.
Option 3: If you use SSL for all your scripts, but not the pages then you will get Mixed Content alerts. Serving all your content over SSL will have implications for caching and performance (but is generally a good idea for any part of a site that needs the user to be logged in).