SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    headers, which format to use?

    In the book PHP & MySQL: For Dynamic Websites (2003, Larry Ullman) he writes headers like this:
    PHP Code:
    header ("Location: http://" .$_SERVER['HTTP_HOST'] .dirname($_SERVER['PHP_SELF']) . "/index.php"); 
    In a later book by the same author (PHP For the World Wide Web, 2nd edition) it is simplified to:
    PHP Code:
    header ("Location: index.php"); 
    Are there any forseeable reasons not to use the simplified version? I need to go through and edit about 25 pages to fix my previously stupidly created headers, so I would like to do it correct the second time.

  2. #2
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I read somewhere before that you should always use absolute paths in header() but I have rarely seen people follow this rule

    Mark.

  3. #3
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mark_W
    Hey,

    I read somewhere before that you should always use absolute paths in header() but I have rarely seen people follow this rule

    Mark.
    Well my site is currently on a temporary domain, and hidden within a password protected directory. Once it goes lives in a few months it'll be on a new domain and not be within a directory. If i have code like
    Location: http://www.exactdomainname.com/exactfolder/index.php
    then i'll have to change every single line once the domain name changes.

  4. #4
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gibb
    Well my site is currently on a temporary domain, and hidden within a password protected directory. Once it goes lives in a few months it'll be on a new domain and not be within a directory. If i have code like
    Location: http://www.exactdomainname.com/exactfolder/index.php
    then i'll have to change every single line once the domain name changes.
    Yeah, it seems pretty pointless to use absolute paths in header(). I was just saying that I read that is what should be done.

    Mark

  5. #5
    SitePoint Zealot kamm's Avatar
    Join Date
    Nov 2003
    Location
    Espaņa
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've almost always used relative URIs, but yesterday when researching headers I came accross this:
    Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
    http://de3.php.net/manual/en/function.header.php

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reasons to use absolute URIs are:

    1) this is a standart (we LOVE standarts dont we? )
    2) some clients don't understand relative URIs (and shouldn't)
    3) some webservers are too 'smart' and try to handle such redirects by themselves, which is generally undesirable.

    Generally there are several things you have to do when you're redirecting:

    - complete relative uri to absolute
    - append SID to uri if you're using sessions
    - send "location" header
    - exit immediately

    It's convenient to write your own "redirect()" function which takes care about this and call it instead of header("location:...")


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
  •