Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
Thread: Weird "include" error.
07-29-2008, 04:38 AM #1
Weird "include" error.
include 'index.php'; // For some reason, it thinks this index.php is the index.php which included template/order.php
07-29-2008, 05:22 AM #2
- Join Date
- Oct 2006
- Denver, Colorado USA
- Thanked 316 Times in 308 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.
07-30-2008, 04:20 AM #3
Is there a way to bypass this? Since I also want to include files below a directory;
include '/../config.php'; // OR include $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
But it isn't working; I get an error/Array() insteadPHP Code:
$array = query('SELECT * FROM `table`');
Last edited by Apothem; 07-30-2008 at 04:24 AM.
07-30-2008, 04:57 AM #4
- Join Date
- Jun 2008
- Redmond, WA
- 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
07-30-2008, 05:53 AM #5
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.
07-30-2008, 03:33 PM #6
- Join Date
- Mar 2006
- Sol System
- 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.
07-30-2008, 05:09 PM #7
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.
07-31-2008, 01:38 AM #8
- Join Date
- Mar 2007
- Florida, USA
- Thanked 406 Times in 398 Posts
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 01:41 AM.