Felix Perdana
Felix Perdana

Reputation: 105

absolute path source php

This is maybe just a trivial question, but I don't really know what is the best practice to include, says javascript, img, or css using absolute path

What I am really using right now is using the code like this

<?php
$prefix = '//';
$rootFolder = $prefix . $_SERVER['HTTP_HOST'];
?>

so then when i want to include something like jquery, I would just type the code like this

<script type="text/javascript" src="<?php echo $rootFolder ?>/jquery-1.7.1.min.js"></script>

is this good enough? Or should I modify the prefix to "http://" or maybe there is some better way using another superglobal variable and such?

thanks in advance :)

Upvotes: 1

Views: 739

Answers (2)

Sarke
Sarke

Reputation: 3245

Use src="/jquery-1.7.1.min.js", the leading slash will go to the root. So it doesn't matter which folder level you are in, it will always find it. This is basically absolute but without the protocol and domain name.

If you are always on the same server, you don't need the host name at all (it's best not to include it if you ever move domain).

Upvotes: 1

Starx
Starx

Reputation: 79049

This question cannot be answered in a correct way, because absolute and relative paths, both have their own importance on a website.

For search engines, they dont like websites with absolute links to the same websites. The modern browsers nowadays already read the relative path with the websites current URL prefixed, unless "/" is used infront of an URL. So, it should not harm when you try to use a relative path as much as possible.

Another case is, If you application is extendable by multiple modules. Generally taking, an admin module and one user module. If these module share a common resource folder for JS, CSS and others then its useful and easy to create a relative paths.

Since, your question is concerned about accessing a resource from the root folder, you should be fine as it is i.e using relative paths.

Upvotes: 1

Related Questions