PDA

View Full Version : Relative or absolute URLs in PHP



belsidaam
11-29-2006, 12:39 PM
Hi evrybody. A very very rooky question to which I did not find an easy answer:
I sometimes get problems when I use the include function to add headers or the like. Somehow he does not accept absolute URLs (e.g. /includes/header.php)
Am I doing something wrong?
In general, also for HTML links and images. Should I use relative (e.g. ../pics/image.png) or should I always stick with absolute?
What is the best if, lets say, I want to move the complete site or just a subfolder?
Disadvantage or advantage of relative/absolute links?

Thanxs for your replies.

BLiZZaRD
11-29-2006, 12:56 PM
I have always used absolute, Just need to make sure your code is correct through out.



<?php include 'folder/file.php'; ?>


Works for me :)

manisha007
11-29-2006, 01:37 PM
absolute URL is the best one. I always use absolute urls.

belsidaam
11-29-2006, 02:21 PM
Thanks a lot I'll try to stay with one. What if the include is for files in different folders but the inlcude itself contains always a link like ../index.php.
For example I have:
/projects/project1/index.php and /contact/index.php which are actual files having includes called by ../../cgi/header.php and ../header.php.
Now my include file contains ../index.php to go back to the main homepage. This would not work for the /projects/project1/index.php file. I would have to put /index.php in the include command?
Also. If I do not want to always calculate how many ../ I have to put, I would like to se /cgi/header.php. But then he did not find the header.php.
Probably just making sure that the reference is correct and not being lazy is the best.

(BTW, if you look at my page. I just saw that the IE specific css is not available. Might look strange. Sorry. Never work late in the night and then forget to load your stuff to the server. Could be embarassing when you start bragging the next day to your friends.)

BLiZZaRD
11-29-2006, 03:01 PM
Try using the full absolute URL, you can also use () and double ' - " to tell the server where exactly to look. something like:



<?php include ("/home/usr/public_html/cgi-bin/file.php"); ?>


Works good that way too. :)

Twey
11-29-2006, 04:41 PM
Firstly, to clear up some definitions: a URL (Uniform Resource Locator) is a string which specifies exactly how to access a given resource. It starts with a protocol, followed by a colon and two slashes.
<?php include 'folder/file.php'; ?> This is not an URL. This refers to a local file, and doesn't include a protocol. It's called a path. Moreover, it's a relative path :p

Relative paths are always preferable, since one can move the whole site to another location without modifying anything. There are few advantages to absolute paths. Generally speaking, the only time one would want to use an absolute path is when one intends to move individual pages about, in which case one is probably modifying the site anyway, so doing a sed(1 (http://www.opengroup.org/onlinepubs/007908799/xcu/sed.html)) is usually not too much of a hassle.

belsidaam
11-30-2006, 08:01 AM
Thanks both of you. I'll definitely try with the brackets.

Cheers

BLiZZaRD
11-30-2006, 08:13 AM
Great information Twey, as usuall. A wealth of knowledge can be gained from you if one is paitient enough to lurk in your shadows and pick up the crumbs you (un?)intentionally leave :p

Care to throw the def. of URI into the mix for those of us salivating for a "normal person's" definition of such items? I read the def's and it's still encoded too much into Geek Code for me to completely decypher :D

djr33
11-30-2006, 08:50 AM
URLs/full paths are also preferable when outputting headers.
There are a few (limited) examples of situations like this.

mwinter
11-30-2006, 01:53 PM
Firstly, to clear up some definitions: a URL (Uniform Resource Locator) is a string which specifies exactly how to access a given resource.

A URL specifies the location of a resource. How to access it is only implied by the scheme. There's a subtle difference.



It starts with a protocol, followed by a colon and two slashes.

An absolute URL (or URI) begins with a scheme name followed by a colon, the latter delimiting the scheme from the rest of the URL. In fact, that's the only syntactic component that makes the difference between an absolute and a relative URL. The two slashes are part of a different component and denote the authority: typically a name to be resolved into an address. URLs can be relative, and may contain any or all of the authority, path, query, or fragment components.




Care to throw the def. of URI into the mix for those of us salivating for a "normal person's" definition of such items?

A URI is a Uniform Resource Identifier. There aren't many ways of describing what a URI is: it is a uniform (regular, consistent, structured) means of identifying (naming) a resource (thing). A URL is also a URI, except that the former also implies where that resource is, not just providing a way to identify it.

There are other things in the wider world that are similar to URIs. Consider ISBN. This is similar in that it's a way of identifying books. In fact, an ISBN can be represented as a URI:

&#160;&#160;urn:isbn:0-140-27406-5

URIs don't need to be hierarchical: there doesn't need to be levels or directories. In such cases, the "path" is just a string of characters. Here, the path value is "isbn:0-140-27406-5".

Another example is XML namespaces. These take the form of a URI:

&#160;&#160;http://www.w3.org/2001/XMLSchema

The address itself doesn't actually mean anything in the usual sense; there doesn't have to be a website at the other end. It's just a way of identifying that namespace. However, it's quite convenient in this case: the W3C control the w3.org domain, so any namespace they create can use that and they don't have to worry about clashes.

That probably doesn't help much, but as I wrote, there's not much to explain.

Mike

BLiZZaRD
11-30-2006, 03:09 PM
Little better :D Thanks for that.

djr33
11-30-2006, 03:31 PM
So.... URIs include all URLs, but not all URIs are URLs? And all paths* are both?

(*In the sense of an address/path related to the location (relative or absolute) of a file/document on the web/in programming/coding/scripting.)

mwinter
12-01-2006, 12:16 AM
So.... URIs include all URLs, but not all URIs are URLs?

Yes. URLs are a subset of URIs.



And all paths* are both?

(*In the sense of an address/path related to the location (relative or absolute) of a file/document on the web/in programming/coding/scripting.)

I'm not quite sure where you're going with that. A path in a file system, for instance, isn't the same as a URI path. They might look the same, and relative file system paths might even resolve against a base path (the working directory) in the same way that a relative URI resolves against a base URI, but they aren't the same thing.

Mike