SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 30
  1. #1
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    PHP Includes and root variable

    First off I googled a million pages and just could not find answers .

    I am trying to do this:
    1. In a configuration.php file I want to set up the path to my site
    I currently have this
    Code PHP:
    <?php
     $siteroot = $_SERVER['DOCUMENT_ROOT'];
    ?>

    2. In my other files I want to simply create blank containers and just include pages. So for instance in Index.php I want to include header.php, sidebar.php and footer.php.

    I currently have this:
    At the top of my page I am calling the configuration.php: <?php require_once ("includes/configuration.php");?>

    And in the spot where I want my header to appear I use this:
    Code PHP:
    <?php include("$siteroot/includes/module-header.php"); ?>

    And likewise for all the other includes - sidebar.php and footer.php.

    This does not work, I get this error message:
    Code HTML4Strict:
    [b]Warning[/b]:  include(D:/wamp/www//includes/module-header.php) [[url="http://localhost/imi/function.include"]function.include[/url]]:  failed to open stream: No such file or directory in [b]D:\wamp\www\imi\index.php[/b]  on line [b]105[/b]

    Now I want this to work in my local server and seamlessly work in production.

    I have no understanding and what code I am using was from one of my many google adventures... it apparently is incorrect.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $siteroot rtrim($_SERVER['DOCUMENT_ROOT'],'/'); 
    Try that, It looks like the problem lies with multiple directory separators:

    include(D:/wamp/www//includes/module-header.php)

    If you would like to access that variable from within classes or functions its probably best to make it a constant.

    PHP Code:
    define('SITE_ROOT',rtrim($_SERVER['DOCUMENT_ROOT'],'/')); 

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suggest using a configuration file that where you would define the path:

    PHP Code:
    define('YOUR_BASE_DIR''D:/wamp/www'); 
    In your script, you would perform your include like this:

    PHP Code:
    require_once(YOUR_BASE_DIR "/includes/module-header.php"); 
    Defining the path is always better than relying on what can otherwise be a moving target. Avoid relative paths whenever possible.
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  4. #4
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by oddz View Post
    PHP Code:
    $siteroot rtrim($_SERVER['DOCUMENT_ROOT'],'/'); 
    Try that, It looks like the problem lies with multiple directory separators:

    include(D:/wamp/www//includes/module-header.php)

    If you would like to access that variable from within classes or functions its probably best to make it a constant.

    PHP Code:
    define('SITE_ROOT',rtrim($_SERVER['DOCUMENT_ROOT'],'/')); 
    I will not be using it within classes or functions. My needs are to simply have a variable that I can use instead of a path.

    The example you gave does not work - I yet get that error message.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code I provided above will work as we use this as a standard practice for our system configuration.

  6. #6
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by inclick View Post
    I would suggest using a configuration file that where you would define the path:

    PHP Code:
    define('YOUR_BASE_DIR''D:/wamp/www'); 
    In your script, you would perform your include like this:

    PHP Code:
    require_once(YOUR_BASE_DIR "/includes/module-header.php"); 
    Defining the path is always better than relying on what can otherwise be a moving target. Avoid relative paths whenever possible.
    That works - so when I take this to production, should the base directory change to http://www.sitename.com/" or the server path? If either one will work which is recommended?

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by conradical View Post
    That works - so when I take this to production, should the base directory change to http://www.sitename.com/" or the server path? If either one will work which is recommended?
    When you go to production, you would use the server path to the file (the FULL path is advised).

    Glad to see that worked.

    -Bing
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  8. #8
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Thank you. I have one more question.

    So if I want to now use that variable (YOUR_BASE_DIR) in a link how would I call it?

    For example i want to link to a file in information/faq/questions.php

    How would i call the variable -
    <a href="<?php What php code ?>/information/faq/questions.php">FAQ</a>

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You wouldn't, you would need to define a new constant:

    PHP Code:
    define('YOUR_WEB_URL''http://www.somdomain.net'); 
    You would the write your link like this:

    <a href="<?php echo YOUR_WEB_URL; ?>/information/faq/questions.php">FAQ</a>
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  10. #10
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by inclick View Post
    You wouldn't, you would need to define a new constant:

    PHP Code:
    define('YOUR_WEB_URL''http://www.somdomain.net'); 
    You would the write your link like this:

    <a href="<?php echo YOUR_WEB_URL; ?>/information/faq/questions.php">FAQ</a>
    Oh sweet! Thanks.

  11. #11
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need a constant. / is already defined for you.

    <a href="/index.php">
    will always go to http://domain.com/index.php

  12. #12
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    You don't need a constant. / is already defined for you.

    <a href="/index.php">
    will always go to http://domain.com/index.php
    While this is true, it is a best practice to not use or depend on relative URLs.
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  13. #13
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by inclick View Post
    While this is true, it is a best practice to not use or depend on relative URLs.
    It's not a relative url

    <a href="index.php">
    => http://site.com/current/directory/index.php

    <a href="/index.php">
    => http://site.com/index.php

    And to your other comment re php paths, it is actually best practice to depend on a relative path to define your root.

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Defining the base URL is still recommended, particularly when you start scaling out to multiple servers or use a CDN. If you have no plans to do that, then sure, you could simply use the relative href.
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  15. #15
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not recommended at all, and completely pointless to boot.

  16. #16
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by hash View Post
    It's not recommended at all, and completely pointless to boot.
    Okay - what be be your suggestions to my question?

  17. #17
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this looks like what i've been looking at in this thread. i would need to include that configuration.php in every php file no matter what folder it is? that would be using relative paths i imagine.. just getting utterly confused trying to solve a pretty similar problem..

  18. #18
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    It's not a relative url

    <a href="index.php">
    => http://site.com/current/directory/index.php

    <a href="/index.php">
    => http://site.com/index.php

    And to your other comment re php paths, it is actually best practice to depend on a relative path to define your root.
    How do you figure those aren't relative? href="index.html" == "./index.html" give me index of current directory.
    If you are at http://site.com/current/directory/index.php and link to /some/dir/example.php it knows you mean http://site.com/some/dir/example.php not http://site.com/current/directory/some/dir/example.php cause a single / means go to root then find x
    Ryan B | My Blog | Twitter

  19. #19
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by conradical View Post
    Okay - what be be your suggestions to my question?
    For html you just use /
    <a href="/some/directory/img.php"> <-- that url is the absolute path from the web servers root
    <a href="img.php"> <-- that url is relative to what directory you are currently in

    For php paths, you are no longer talking about the web server root, but the servers file system. So you need $root to be the path to your project, eg /home/hash/websites/myproject/
    You can set this based off the DOCUMENT_ROOT, or based on an entry point in your script, or other ways, like hard coding a config file.

  20. #20
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    How do you figure those aren't relative? href="index.html" == "./index.html" give me index of current directory.
    If you are at http://site.com/current/directory/index.php and link to /some/dir/example.php it knows you mean http://site.com/some/dir/example.php not http://site.com/current/directory/some/dir/example.php cause a single / means go to root then find x
    The first was relative for illustration of why the second wasn't. I bolded the bit in your post which means absolute.

  21. #21
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    The first was relative for illustration of why the second wasn't. I bolded the bit in your post which means absolute.
    ah ok, my bad
    Ryan B | My Blog | Twitter

  22. #22
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @hash
    When I was referring to the href, the following are relative URLs:
    index.html -> Relative to the base URL and current directory
    /index.html -> Relative to the base URL
    ./index.html -> Relative to the base URL and parent directory
    Anything that is coded without the base URL of the site is relative.

    When you are using relative URLs in your links, the base URL is assumed. Thus, the following have the identical behavior:
    <a href="index.php"> <-This is relative
    <a href="http://www.somedomain.com/index.php"> <-- This is absolute

    <a href="/some_dir/index.php"> <-This is relative
    <a href="http://www.somedomain.com/some_dir/index.php"> <-- This is absolute

    Depending on the scope of the project, relative URL references may be fine. In some instances (all in my case), however, that simply is not the case. To simplify management for smaller sites, relative URLs can work. If you are developing an application that others will install in their own environments, plan on moving files/directories around, use a Content Data Network (again, this has it s own use cases that can differ), then you should definitely use absolute URLs whenever possible. Another use case, using mod_rewrite for content. By using absolute paths, content can live in what appears to be separate URLs without breaking image / css references. I won't get into that as this will totally veer this thread into the wrong direction since it is about PHP file paths.

    With regard to paths in your PHP code, I am a strong advocate of using absolute references. From being able to internally audit locations, to enforcing security policies, using absolute references just makes sense. I recommend to all new PHP coders to get in the habit of using absolute references whenever possible. Like the HTML use cases above, this also depends on the scope of the project. This can not only makes things clear as to where things go, but also allows for configuration changes on a per deployment basis. This is particularly true when a bulk of the PHP code could live outside of the public HTML space, including configuration files (think security). If your script uses cron jobs, the use of relative path's will lead to a lot of frustrated users and headaches for you.
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  23. #23
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh no. Path question again. With same rumors and misunderstandings. Sitepoint desperately need an article on what is a path and what is a file.

    conradical
    The first thing you had to do, is to determine the real path to your module-header.php file. As you have access to whole disk, it is not too hard, right?
    So, your goal then would be limited just to producing a string, equal to that standard. Much better than guessing.
    And string comparing is not too hard too.

    Now you ended up with hardcoded path instead of automated one. Not the best choice.
    Why don't you just var_dump() both working and not working paths and just visual compare it? And then correct the automated one? To use on both servers unchanged?

  24. #24
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @inclick, you've smooth words, but I prefer a code.

    . If you are developing an application that others will install in their own environments,
    ...they'll end up with your domain in their links.
    plan on moving files/directories around,
    so what? how do your base url thing will help with it?
    use a Content Data Network
    your base url constant won't help with it

  25. #25
    SitePoint Zealot
    Join Date
    Feb 2009
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would love to see a tutorial about this issue particular issue.. including absolute (rather than relative to accomodate includes inside includes) paths in a relative way (ie transportable from test server/localhost to live deployment). it seems to be asked a million places online all over the place and i don't see an clear step by step solution anywhere.


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
  •