gpt4 book ai didi

php - 如何构建 PHP 内容以用于非安全 (http ://) and secure (https://) areas and across multiple directories?

转载 作者:可可西里 更新时间:2023-11-01 00:24:49 24 4
gpt4 key购买 nike

我有一个包含链接和图像的页脚文件。该文件用于我的主页和多个目录中。用户登录后,它还会用于我网站 (https://) 的安全部分。

组织链接的最佳方式是 (1) 它们可以用于我网站的非安全 (http://) 和安全 (https://) 区域,同时 (2) 也能够在我的网站的不同目录中使用 include 吗?

似乎为了满足我的第一个要求(1),我不得不使用相对链接;但是,为了满足我的第二个要求 (2),我需要使用绝对链接。

如果您能提供任何帮助,那就太好了!

<div id="footer">
<a href="http://www.sample-link.com" target="_blank">
<img id="sample-image" src="http://<? print $_SERVER['SERVER_NAME'] ?>/media/sample-image.png" />
</a>
</div>

最佳答案

只需使用 //example.com/foo.blah .

<img id="sample-image" src="//<? print $_SERVER['SERVER_NAME'] ?>/media/sample-image.png" />

很明显 HTTP 是协议(protocol),但还存在其他协议(protocol),例如 GOPHER、HTTPS、FTP 等...

而不是在地鼠 URL 上 gopher://example.com你可以简单地链接到 //example.com协议(protocol)在没有它的情况下也能工作。如果您想从 HTTPS 链接到 HTTP 内容,您需要指定协议(protocol)。

从长远来看,这可能会很有趣,例如,如果出现另一个与 HTTP 几乎相同的协议(protocol)(例如,谷歌正在尝试制作 spdy:// ),您将不得不更新所有代码.当您特别需要从该协议(protocol)获取内容时使用该特定协议(protocol),您可能会在 5 年后避免这个问题。

相对 URL 在 URL 的 RFC 中描述,RFC3986 :

https://www.rfc-editor.org/rfc/rfc3986#section-4.2

4.2. Relative Reference

A relative reference takes advantage of the hierarchical syntax(Section 1.2.3) to express a URI reference relative to the name spaceof another hierarchical URI.

   relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

relative-part = "//" authority path-abempty
/ path-absolute
/ path-noscheme
/ path-empty

The URI referred to by a relative reference, also known as the targetURI, is obtained by applying the reference resolution algorithm ofSection 5.

A relative reference that begins with two slash characters is termeda network-path reference; such references are rarely used. Arelative reference that begins with a single slash character istermed an absolute-path reference. A relative reference that doesnot begin with a slash character is termed a relative-path reference.

A path segment that contains a colon character (e.g., "this:that")cannot be used as the first segment of a relative-path reference, asit would be mistaken for a scheme name. Such a segment must bepreceded by a dot-segment (e.g., "./this:that") to make a relative-path reference.

要了解“路径”、“权限”和“方案”的含义,请参阅同一 RFC 中的图表:

     foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
| _____________________|__
/ \ / \
urn:example:animal:ferret:nose

所有这些都是假设您正在使用多个域,如有关 <img src="./relative/url/foo.png"> 的问题或 img src="/absolute/path/while/domain/relative/foo.png"已被回答多次

关于php - 如何构建 PHP 内容以用于非安全 (http ://) and secure (https://) areas and across multiple directories?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9080730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com