SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Starting off right with file includes

    Hello!

    On my first PHP project, I coded all of my include/require files as:

    PHP Code:
    require_once(../../main/restrict_access_index.php'); 
    It's kind of a pain and not super clear coding in my opinion. Diving into my second PHP project, I was hoping to use the $_SERVER global variable and DOCUMENT_ROOT as below:

    PHP Code:
    require_once($_SERVER["DOCUMENT_ROOT"].'/main/restrict_access_index.php'); 
    Is this better coding practice or might there be another solution that I'm not aware of?

    Thank you,

    Eric

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,114
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by kreut View Post
    PHP Code:
    require_once($_SERVER["DOCUMENT_ROOT"].'/main/restrict_access_index.php'); 
    Is this better coding practice or might there be another solution that I'm not aware of?
    Personally, I much prefer that solution, as you can then use the same link anywhere without having to worry about relative paths.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for the quick reply!

    -Eric

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ralph,

    I've now given the whole $_SERVER["DOCUMENT_ROOT"] some thought and now think that there may be one small issue. Let's say I develop the site for www.thebestsiteever.com. However, it turns out that it needs to be all moved to a subdirectory (this is actually a possibility for the project), so that all of my files now reside in www.thebestsiteever.com/thebestfolderever/. If this does happen, wouldn't I have to change all of my $_SERVER["DOCUMENT_ROOT"] to reflect this with the additional layer? And, if this is the case, might there be a more "flexible" solution?

    Thanks again...

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kreut View Post
    If this does happen, wouldn't I have to change all of my $_SERVER["DOCUMENT_ROOT"] to reflect this with the additional layer? And, if this is the case, might there be a more "flexible" solution?
    How about this?
    Code:
    require_once($_SERVER["DOCUMENT_ROOT"] . $SITE_DIR . '/main/restrict_access_index.php');
    Just set a global $SITE_DIR (or whatever name you like) to an empty string. If/when the site moves, simple modify that global variable!
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)


    Thank you...

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recently started using constants for this. In my configuration file i declare constants with the right paths. If site moves I change soms constants.
    But I don't know If this is good practise


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
  •