SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    North Carolina
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP includes an absolute links in folders

    Hi all,

    I just set up a site with php includes to easily update nav, sidecolumn, and footer information. I used dreamweaver templates, with php includes in the templates. This was going ok until the client decided to have pages generated from the templates into subfolders on the site. The templates were only made to create a file in the root directory, then read the include from a php folder, like this:

    mainfile.php(has an include that includes)
    php(folder)/nav.php

    My problem is, when they create a file in a subfolder, the links in the navigation no longer work because they are trying to find the files in the subfolder instead of the main directory...

    This is all fine and wonderful, because to work around it I used absolute links in the navigation (instead of linking to file.php, I liked to http://www.site/file.php). And everything was working fine...

    until...

    The client decided to create a "betasite" testing folder for a large round of changes. She had to go into my included php files with the absolute links to betatest, then take them out to make the changes live.

    I hate for them to have to do this. Is there an easier way to set this up?

    I hope my post wasn't too confusing.


    j
    Jon
    www.jonhebert.com
    Web design & development +
    Print & graphic design

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you remove the host you won't have that issue. ie:

    /file.php

    will link to http://www.site.com/file.php on the live site or http://beta.site.com/file.php on the beta site. That would solve all the issues there.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    North Carolina
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response.

    I might have to clarify a bit, or maybe I'm confused. The issue is that, for example, nav.php , with relative links, acts ok if included in a file in the root directory. However, when you include it in a file within a folder in the root directory, the links no longer work, as they are trying to look for the linked file within the folder, although it is in the root directory.

    Setting all of the links to absolute links in the nav solved the problem, but creates a new problem with the betasite. I would like my client to be able to setup a beta site without having to redo the links in the navigation everytime they want to create the betasite then switch back.

    I hope I'm making myself clear on this! I'm not a programmer, just a php novice!
    Jon
    www.jonhebert.com
    Web design & development +
    Print & graphic design

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The example I gave of linking is still absolute. This method uses the DOCUMENT ROOT, which is a variable used in most web servers to indicate where to serve files from. By saying:

    /mypage.php

    no matter where you are in the site, that syntax is saying "Look for mypage.php in the topmost level of the document root". So if you have:

    Production document root: /home/production/public_html/
    Beta document root: /home/betasite/public_html/

    Using /mypage.php on the production site would point to mypage.php in /home/production/public_html/ (or something else if you're using mod_rewrite or something) and /home/betasite/public_html/mypage.php in the production site.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Generally your include files should not come from anywhere beneath your /docroot

    But should be in a folder not available to the public like:

    /home/production/includes

    That folder should then be stipulated in the php.ini file under:

    include_path =

    This can contain more than one path.

    From then on all you need to do is stick nav.php

    in

    /home/production/includes/nav.php

    and just do:
    PHP Code:
    <?php
    include( "nav.php" );
    You can then do the above include in any public folders no matter how nested they are ...

    /home/production/public_html/products/all/index.php

    Most people put information like dbase logins and other global information in the include files outside of your docroot.

    This is the correct way to get included information.

    If you get too many include files then you can stick them in subfolders too:

    /home/production/includes/navigation

    Then reference them so:
    PHP Code:
    <?php
    include( "navigation/ie6.php");

  6. #6
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    One idea is to put a file in root called root.htm
    PHP Code:
    if (file_exists("root.htm")){
    $path = '.';
    }
    else{
    $path = '..';
    }
    <?php echo $path ?>/mypage.php
    Or something like that.
    What I lack in acuracy I make up for in misteaks

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    North Carolina
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been trying some of these with no success yet.

    chris_fuel , would your solution work for files in folders as well? For example, if I set an include file to read from /folder/file.php, then no matter where that include was (in a file in the root, or in a folder) it would always read from the proper place?
    Jon
    www.jonhebert.com
    Web design & development +
    Print & graphic design

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    North Carolina
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    chris_fuel, Your solution looks easiest, but i'm having trouble. what if my root folder looks like the attached screenshot. The site is in a folder in the root folder, but i don't have access to post files in that root folder. The betasite is set up the same way, a folder in the root, but no acess to the root.

    My ftp info I got from the client is set up like this:
    ftp host: IP number
    host directory: www.peoplesbanknc.com/
    user: etc
    pass: etc

    does this make sense? Is this why I can't get files to read from the root directory correctly? How could I fix, this, is it the way IT set it up?

    PS: ignore the "betasite" folder, here, it is not the one I am referring to.

    Thank you!!
    Jon
    www.jonhebert.com
    Web design & development +
    Print & graphic design


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
  •