Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Weird "include" error.

    index.php:
    PHP Code:
    include 'template/order.php'
    order.php:
    PHP Code:
    include 'header.php';
    include 
    'index.php'// For some reason, it thinks this index.php is the index.php which included template/order.php 
    As you can probably see above, order.php is including the index.php which included template/order.php... causing an error.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    Files for including are first looked for in each include_path entry relative to the current working directory (which is the directory of the main script that was requested), and then in the directory of current script (the main script that was requested.)

    I recommend always forming absolute file system paths using $_SERVER['DOCUMENT_ROOT'] and the path/file relative to the document root. This will insure that your include statement will always work no matter where the file with the include() statement is located or what file might be including another file.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Is there a way to bypass this? Since I also want to include files below a directory;

    install/file.php
    PHP Code:
    include '/../config.php'// OR include $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
    print_r($array); 
    config.php
    PHP Code:
    include 'includes/db_func.php';
    $array query('SELECT * FROM `table`'); 
    But it isn't working; I get an error/Array() instead
    Last edited by Apothem; 07-30-2008 at 05:24 AM.

  • #4
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    If you're doing lots of includes it's best to just use a global include file.

    I have a file called common.php in my installations which includes all of the neccessary operating files for each page (global functions, configuration options, language files, template files, etc.). Then if you've got a more specific file (auth_functions, for example) you can include from the neccessary file.

    For the reason stated in the 2nd post it's best to do all your includes at once and avoid them on each page.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #5
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Well, my config.php includes everything function related to run the script. Which is why I'm trying to find a way to bypass the need to repeat include.

  • #6
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    If config.php already has all the includes, and if you include config.php on any other file, then there will be no need to include anything which has been included in the config.php file. For something like this, where there are a lot of includes, I would switch to using include_once(). include_once checks to see of the code from said file has already been included, if it has, then it will not include it again. Kind of like mysql_pconnect().
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #7
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Your right; it just seems that I had an error in a code regarding query().

    Going back to the first problem, I found out that ./ and ../ is supposed to make the includes stick with one folder. Ironically it doesn't.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Pykex View Post
    Your right; it just seems that I had an error in a code regarding query().

    Going back to the first problem, I found out that ./ and ../ is supposed to make the includes stick with one folder. Ironically it doesn't.
    "./" makes the path start in the current folder. "../" moves up one directory and starts there. Your post above has a path that starts with a "/" so the path would start in the root, and then you have "..", which is not logical since you're already in the file root(not document root).. and you can't go any higher.
    Last edited by Inigoesdr; 07-31-2008 at 02:41 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •