如何构造PHP的内容包括在非安全(http://)和安全(https://)区域以及跨多个目录使用?
作者:互联网
我有一个包含链接和图像的页脚文件.该文件在我的主页和多个目录中使用.在用户登录后,它也会在我网站的安全部分(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是协议,但存在其他协议,如GOPHER,HTTPS,FTP等……
而不是在gopher URL gopher://example.com上,您只需链接到//example.com,协议就可以在没有它的情况下工作.如果要从HTTPS链接到HTTP内容,则应指定协议.
从长远来看,这可能是有趣的,例如,如果另一个协议出现与HTTP几乎相同的工作(例如,谷歌试图制作spdy://),您将不得不更新所有代码.当您特别需要从该协议获取内容时使用特定协议,您可能会在未来5年内避免此问题.
RFC for URL,RFC3986中描述了相对URL:
http://tools.ietf.org/html/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 space
of 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 target
URI, is obtained by applying the reference resolution algorithm of
Section 5.A relative reference that begins with two slash characters is termed
a network-path reference; such references are rarely used. A
relative reference that begins with a single slash character is
termed an absolute-path reference. A relative reference that does
not 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, as
it would be mistaken for a scheme name. Such a segment must be
preceded 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,include,https,hyperlink,include-path 来源: https://codeday.me/bug/20190729/1574838.html