Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Relative or absolute URLs in PHP

  1. #1
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Relative or absolute URLs in PHP

    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.

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I have always used absolute, Just need to make sure your code is correct through out.

    PHP Code:
    <?php include 'folder/file.php'?>
    Works for me
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    absolute URL is the best one. I always use absolute urls.

  4. #4
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

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

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

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

    PHP Code:
    <?php include ("/home/usr/public_html/cgi-bin/file.php"); ?>
    Works good that way too.
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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

    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) is usually not too much of a hassle.
    Last edited by Twey; 11-29-2006 at 03:46 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks both of you. I'll definitely try with the brackets.

    Cheers

  8. #8
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    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

    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
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    URLs/full paths are also preferable when outputting headers.
    There are a few (limited) examples of situations like this.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    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.


    Quote Originally Posted by BLiZZaRD View Post
    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
    Last edited by mwinter; 11-30-2006 at 01:29 PM. Reason: Responding to BLiZZaRD

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •