...

View Full Version : Weird "include" error.



Apothem
07-29-2008, 05:38 AM
index.php:

include 'template/order.php';

order.php:

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.

CFMaBiSmAd
07-29-2008, 06:22 AM
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.

Apothem
07-30-2008, 05:20 AM
Is there a way to bypass this? Since I also want to include files below a directory;

install/file.php

include '/../config.php'; // OR include $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
print_r($array);

config.php

include 'includes/db_func.php';
$array = query('SELECT * FROM `table`');

But it isn't working; I get an error/Array() instead

ShaneC
07-30-2008, 05:57 AM
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.

Apothem
07-30-2008, 06:53 AM
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.

BWiz
07-30-2008, 04:33 PM
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() (http://php.net/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(). (http://php.net/mysql_pconnect)

Apothem
07-30-2008, 06:09 PM
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.

Inigoesdr
07-31-2008, 02:38 AM
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 (http://www.codingforums.com/showpost.php?p=717753&postcount=3) 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum