#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    51
    Rep Power
    3

    Help with my php include function.


    hi everyone

    i really just want some ideas from everyone as to why my PHP include function is not working.

    i am trying to include a page into the home page of a site.

    the relevant page, for inclusion, is held in another folder on the same site. it held in the following folder :
    /cms/

    so, from the home page i did the following include:

    PHP Code:
     include('/cms/db_fns.php'); 
    this should have linked to the relevant page but it does not work .

    when i however did the following it worked;

    PHP Code:
     include('cms/db_fns.php'); 
    so, i am unclear where i have gone wrong.

    i need to have an absolute root so that i can use the relevant "includes" on any pages on the site, irrespective of which folder they are in.


    ideally, i would like to use a web root

    i.e

    PHP Code:
     include('http://www.thesite.com/cms/db_fns.php'); 
    is there anyway to do this on a site where u cannot get access to the apache server. i.e hosting companies do not allow u access to the apache server to switch it off and on again.

    can someone please advise me where i have gone wrong. thank you so much.

    warm regards

    Andreea
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    please read the documentation on the include statement (it's not a function, so you don't need the parentheses).

    Any path you specify for "include" refers to the file system, not the document root or something. So "/cms" would be the "cms" folder in the root directory of the server (which of course makes no sense).

    What you're trying to do requires a custom php.ini with the include_path set to your "cms" directory. Relative paths will then use this directory.

    However, I'm not sure if this is a good idea. I'd simply specify paths relative to the current script's directory.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    51
    Rep Power
    3
    Originally Posted by Jacques1
    Hi,

    please read the documentation on the include statement (it's not a function, so you don't need the parentheses).

    Any path you specify for "include" refers to the file system, not the document root or something. So "/cms" would be the "cms" folder in the root directory of the server (which of course makes no sense).

    What you're trying to do requires a custom php.ini with the include_path set to your "cms" directory. Relative paths will then use this directory.

    However, I'm not sure if this is a good idea. I'd simply specify paths relative to the current script's directory.

    HI again everyone and special thanks to Jacques1 for his response.

    i am a little bit confused by the answer though.

    are you saying that i am not suppose to use the slash symbol i.e /

    or i am not suppose to use the " "

    is it possible for you or someone to give me an example of how the include should be. .

    i looked at the "include documentation" but its not clear about the syntax for includes where the file is in another folder from the actual page.


    warm regards

    Andreea
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by andreea115
    i looked at the "include documentation" but its not clear about the syntax for includes where the file is in another folder from the actual page.
    This has nothing to do with a special "syntax" or something. You have to understand the difference between web paths and file system paths.

    The "include" statements operates on the file system. So when you are on a Windows machine, it would take paths like "C:\\htdocs\\cms\\index.php". On a Linux machine, it would take paths like "/var/www/cms/index.php". We're talking about actual, physical paths of the operating system here.

    This is completely different from web paths like "/cms/index.php" in the URL "http://www.mycoolsite.com/cms/index.php". Those are "virtual" paths that are resolved relative to the webserver's document root and don't even have to actually exist.

    Again: "include" uses the file system. So when you specify the path "/cms", PHP will actually look in the root directory of your Linux/Windows installation. And I'm pretty sure there is no "cms" folder.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    51
    Rep Power
    3
    Originally Posted by Jacques1
    This has nothing to do with a special "syntax" or something. You have to understand the difference between web paths and file system paths.

    The "include" statements operates on the file system. So when you are on a Windows machine, it would take paths like "C:\\htdocs\\cms\\index.php". On a Linux machine, it would take paths like "/var/www/cms/index.php". We're talking about actual, physical paths of the operating system here.

    This is completely different from web paths like "/cms/index.php" in the URL "http://www.mycoolsite.com/cms/index.php". Those are "virtual" paths that are resolved relative to the webserver's document root and don't even have to actually exist.

    Again: "include" uses the file system. So when you specify the path "/cms", PHP will actually look in the root directory of your Linux/Windows installation. And I'm pretty sure there is no "cms" folder.

    thank you. that is great. i am starting to understand. but i am still not clear what i am suppose to do now.
    is it possible to give an example of how i can write the path?


    regards

    Andreea
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Please don't make a full quote of every reply. The text is right above your post, so no need to repeat it.

    Originally Posted by andreea115
    is it possible to give an example of how i can write the path?
    I said that in my first reply. If you put your "cms" directory into the include_path (read the documentation), you can refer to it with relative paths. For example, if you say
    PHP Code:
    include 'scripts/db.php'
    then "scripts/db.php" will be looked up in your "cms" directory (among others).

    If you have problems understanding how paths in general work, google for "file path".
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Most PHP software handles this by defining a constant that references the root of the application. For example, something like:
    PHP Code:
    define('ROOT''/var/www/html/'); 
    Then throughout the application whenever they need to include something, they use the ROOT constant to do it:
    PHP Code:
    include(ROOT 'libraries/includeme.php'); 
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo