SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Moving Site With PHP Include

    A website that was moved to another server is gettin errors for all the PHP Includes on the site since the include path is now incorrect..

    The path that it had before was
    Code php:
    <?php include('F:\Websites\Site\php\footer.php'); ?>

    and to get them to work I could change it to
    Code php:
    <?php include('/home/site/public_html/php/footer.php'); ?>

    It seems the first location was a windows server or something.. how do I fix this so there isnt a problem if it is moved again? How do I have the include path start from "/php/footer.php" instead of "/home/site/public_html/php/footer.php" ?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    By using the full path you're making it much harder for yourself to transfer the site. Ideally you want a drag-drop transfer - i.e. an FTP transfer and the site should work - maybe with the additional editting of a settings/constants file.

    Try using a relative path instead, i.e.:
    PHP Code:
    include 'footer.php'
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use a relative path from $_SERVER['DOCUMENT_ROOT']. ie. <?php include($_SERVER['DOCUMENT_ROOT'] . '/php/footer.php'); ?>
    MySQL v5.1.58
    PHP v5.3.6

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrandonK View Post
    use a relative path from $_SERVER['DOCUMENT_ROOT']. ie. <?php include($_SERVER['DOCUMENT_ROOT'] . '/php/footer.php'); ?>
    That's absolute path

  5. #5
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternative to $SERVER['DOCUMENT_ROOT'] would be:

    PHP Code:
    define('BASE_PATH'dirname(dirname(__FILE__))); 
    But it's still absolute.

  6. #6
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    That's absolute path
    But the document_root part is dynamic based on the server settings. By using that variable (opposed to typing out c:\web\html\), your code becomes more portable.
    MySQL v5.1.58
    PHP v5.3.6

  7. #7
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrandonK View Post
    But the document_root part is dynamic based on the server settings. By using that variable (opposed to typing out c:\web\html\), your code becomes more portable.
    Still doesn't make it a relative path. It's an absolute path.

  8. #8
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    By using the full path you're making it much harder for yourself to transfer the site. Ideally you want a drag-drop transfer - i.e. an FTP transfer and the site should work - maybe with the additional editting of a settings/constants file.

    Try using a relative path instead, i.e.:
    PHP Code:
    include 'footer.php'
    this would work but on subpages I would have to do something like
    PHP Code:
    include '../footer.php'
    right?

    Quote Originally Posted by BrandonK View Post
    use a relative path from $_SERVER['DOCUMENT_ROOT']. ie. <?php include($_SERVER['DOCUMENT_ROOT'] . '/php/footer.php'); ?>
    I think this is what im looking for..would I have to set the DOCUMENT_ROOT somehow?

    Quote Originally Posted by risoknop View Post
    Alternative to $SERVER['DOCUMENT_ROOT'] would be:

    PHP Code:
    define('BASE_PATH'dirname(dirname(__FILE__))); 
    But it's still absolute.
    this 1 is a little confusing since I don't know 2 much php but overall I'm just looking for the best solution so when I move the files to any server the site doesnt break.. I think relative paths might be more work since the path would change on a subpage..

    which of these 3 would be the best solution?

  9. #9
    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)
    To my mind the best solution is to have your include_path correctly set in the php.ini file.

    for example, on server1 ini file:

    include_path /home/site/includes

    on server2 ini file:

    include_path F:\Websites\includes

    Then you keep your footer.php in the includes directory (outside of your document root) and just do the following, no matter how far down the directories it is needed.

    include 'footer.php' ;

    Let PHP do what it does naturally and stop worrying about paths.

    include_path can take a list of directories, and if you want to get jiggy with it you can create sub-folders in your include file like this:

    /home/site/includes/nav/footer.php
    /home/site/includes/database_credentials.php

    and include the file like so:

    include 'database_credentials.php' ;
    include 'nav/footer.php' ;

    HTH

  10. #10
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recently moved a couple of my sites over to Cups method, but for a long time I always just used $_SERVER['DOCUMENT_ROOT']

    @decowski Yes, the path is absolute, but it makes it very easy to include the proper path. It allows portability because it doesn't matter if your root path changes from c:\www\user to /home/http/vhosts/user/httpdocs

    @enwise $_SERVER['DOCUMENT_ROOT'] is predefined based on your server configuration: http://www.php.net/reserved.variables
    MySQL v5.1.58
    PHP v5.3.6


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
  •