开发者

https detection - pros/cons to these two methods

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?


The protocol-relative // is the most intuitive way of handling this. I am not certain it is universally supported by older browsers, however.

<script type="text/javascript" src="//myUri.com/script.js"></script>

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 the HTTPS server variable:

if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {}


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜