Search code examples
phphttpshyperlinkincludeinclude-path

How to structure content of PHP includes for use in both non-secure (http://) and secure (https://) areas and across multiple directories?


I have a footer file that includes links and images. This file is used on my main homepage, and in multiple directories. It is also used on the secure part of my website (https://) after a user signs in.

What is the best way to organize the links so that (1) they can be used in the non-secure (http://) and secure (https://) areas of my site, while (2) also being able to use the include in different directories throughout my site?

It seems like that in order to satisfy my first requirement (1), I'd have to use relative links; however, in order to satisfy my second requirement (2), I'd need to use absolute links.

Any help you could provide, would be great!

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

Solution

  • Just use //example.com/foo.blah.

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

    Quite obviously HTTP is the protocol, but other protocols exist such as GOPHER, HTTPS, FTP, etc...

    Rather than being on the gopher URL gopher://example.com you could simply link to //example.com and the protocol would work without it. If you wanted to link to HTTP content from HTTPS, you would specify the protocol.

    This may be interesting in the long run, for instance, should another protocol come along that works almost the same as HTTP, (for instance, google is attempting to make spdy://) you will have to update all of your code. Use the specific protocol when you specifically need to get content from that protocol and you might avoid this problem 5 years down the road.

    Relative URLs are described in the RFC for URLs, 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 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.

    To understand what they mean by "path", "authority" and "scheme", refer to the diagram in the same RFC:

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

    All of this is under the assumption that you are working with multiple domains, as questions about <img src="./relative/url/foo.png"> or img src="/absolute/path/while/domain/relative/foo.png" have been answered many times.