如何让 PHP 网站同时支持 HTTP 和 HTTPS?

发布于 2024-12-05 09:28:21 字数 293 浏览 2 评论 0原文

我有一个网站,其编写假设 http:// 永远是唯一的协议。现在我购买了 SSL 证书,但是当我访问使用 https:// 调用它的网站时,我在浏览器中收到信息,表明网站的一部分不安全。我发现我有一些 JS、CSS 以及图像和文件,我在网站的 HTML 中使用 http:// 引用它们。

那么启用完整 https 的最佳实践是什么?当我引用图像、CSS 或 JS 时,是否应该在每个地方更改我的网站,检查网站是否加载了 http 或 https,并根据协议加载资源?这对我来说似乎是一项繁重的工作,而且容易出错。有没有其他更简单的方法让整个网站完全安全?

I have a website that was written assuming http:// is one and only protocol forever. Now i bought a SSL certificate but when i visit site calling it with https:// i get info in browsers that part of site is insecure. As i found i have some JS, CSS and images and files that i refer to using http:// in the HTML of the site.

So what is best practice to enable full https? Should i change my website in every place when i refer to image, CSS or JS, check if site was loaded with http or https and load the resource with according protocol? It seems like a lot of work for me and bit error prone. Is there any other way, easier to make the whole site fully secure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和影子一齐双人舞 2024-12-12 09:28:21

而不是使用 http://yoursite.com/css/file.css 链接到您的 css、js 和图像 只需使用相对路径,例如 /images/image.jpg 和 /css/file.css,这样它就可以与 http 和 https 一起使用,而且如果您更改域或将内容复制到另一个域,您应该'也不必更改所有这些链接。

Rather than linking to your css, js, and images with http://yoursite.com/css/file.css just use relative paths such as /images/image.jpg and /css/file.css this way it will work with both http and https, also if you change domains or copy your content to another domain, you shouldn't have to change all those links either.

晨与橙与城 2024-12-12 09:28:21

使用相对路径。如果您指向的内容与您的网站位于同一网站上,那么您不应该使用 http://

如果由于某种原因您仍然需要 http:// 那么只需将它们全部切换为 https:// 即可。 http:// 永远不会抱怨,因为它指向 https:// 内容,但 https:// 页面如果指向非 https 内容就会抱怨。

如果您指向无法控制的内容(例如在另一个网站上),那么您需要希望可以通过 https 获取该内容。如果不能,那么您就完蛋了,您要么需要忍受错误,从其他地方获取内容,要么通过您自己的 https 连接代理内容。

Use relative paths. If you are pointing to something that is on the same site as yours, then you should not be using http://

If for some reason you still need to have http:// then just switch them all to https://. An http:// will never complain because it is pointing to https:// stuff, but an https:// page will complain if it is pointing to non-https stuff.

If you are pointing to content outside of your control, on another site for example, then you need to hope that you can get at that content via https instead. If you can't, then you're hosed and you either need to live with the error, get the content from somewhere else, or proxy the content through your own https connection.

花伊自在美 2024-12-12 09:28:21

为了补充 @drew010 的答案,您可以使用其他域并仍然使用 // 引用当前协议,例如:

<img src="/pics/home.png" />
<img src="//my-cdn.com/pics/info.png" />

后一个示例将指向 https://..< /code> 来自 https://your-site.comhttp://... 来自 http://your-site.com代码>.

To complement @drew010 's answer, you could use other domains and still refer to the current protocol with //, something like:

<img src="/pics/home.png" />
<img src="//my-cdn.com/pics/info.png" />

The latter example will point to https://.. from https://your-site.com and http://... from http://your-site.com.

长亭外,古道边 2024-12-12 09:28:21

最佳实践是使用相对路径而不是绝对路径,但有时绝对路径是更好的选择,因此您可以执行以下操作:

正如我可以想象的那样,您有一个名为 config.php 或 common.php 的文件(一个存储常用的文件) vars 并且您将其包含在每个页面中),因此将此代码放在那里:

function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' 
    : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}

function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}

然后您可以分配一个名为 $http 的 var 来获取函数的值,例如:
$http = selfURL();
然后每当你想包含图像、CSS 等内容时,请执行以下操作:

此方法非常可靠,因为它在任何情况下都有效。

the best practice would be either using relative path rather than absolute but sometimes absolute is a better option so you can do the following :

as I can imagine you have a file called config.php or common.php (a file that stores your common used vars and you include it in every page), so put this code there :

function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' 
    : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}

function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}

and then you can assign a var called $http to get the value of the function like :
$http = selfURL();
and then whenever you want to include anything like images, css, etc do something like :
<img src="<?=$http?>images/sample.png" />

this method is reliable as it works in any situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文