Forum Moderators: coopster

Message Too Old, No Replies

include() using full path

variables not in scope

         

migthegreek

4:43 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



I would like to include files using an absolute path, so that I don't have to rely on relative paths. However, I am finding that when I use an absolute path, any variables in the included file are not globally visible.

e.g.

-------------------------------------------------------
include($_SERVER['PHP_SELF'] . '/path/to/file.php');

echo $variableInFile; // Variable not found
-------------------------------------------------------

Is there any way to include a file with an absolute path and still have the variables visible globally (some workaround using sessions is overkill).

andrewsmd

10:21 pm on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it your own personal server? You could try include("c:\\some folder\\somefile.php");

idfer

11:21 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



It shouldn't make any difference whether you use absolute or relative paths. Are you sure you have the correct path? Make sure you have error reporting on.

Also the example you give doesn't look right to me, usually $_SERVER['PHP_SELF'] points to a file but you're using it as a directory name in your include statement. Perhaps you meant $_SERVER['DOCUMENT_ROOT'].

migthegreek

9:05 am on Jul 21, 2009 (gmt 0)

10+ Year Member



Sorry about that. I don't know why I wrote PHP_SELF (I had been building forms all day). Yes, I meant DOCUMENT_ROOT.

OK, I just realised why absolute paths are probably affecting the variable scope. I am being a bit stupid here. I will explain: I'm actually trying to use a function that allows me to specify absolute paths for includes.

----------------------------------------------------------------------------------------------

// http://www.example.com/includes/config.php

define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('ROOT', 'http://www.example.com/');
define('ORDER', ROOT . 'order/');

// http://www.example.com/includes/functions.php

function abs_include($path, $once = false) {
$path = str_replace(ROOT, DOC_ROOT, $path);
if ($once) {
include_once($path);
} else {
include($path);
}
}

// http://www.example.com/order/file.php

$myVar = 'Hello';

//************** USAGE

abs_include(ORDER . 'file.php');
echo $myVar; // Not found

----------------------------------------------------------------------------------------------

To explain briefly, config.php is always included with every page, and contains some constants which link to important directories. The reason for the abs_include() function is that I want to be able to use those constants when specifying includes, but I have to use $_SERVER['DOCUMENT_ROOT'] in an include() otherwise it won't find the file correctly. So the function allows me to specify a standard absolute URI to include, such as 'http://www.example.com/order/file.php'

idfer

3:51 pm on Jul 21, 2009 (gmt 0)

10+ Year Member



Ah that's different, you're including the file inside the function abs_include() so all the variables declared in the included file become local to that function, not global. It's better to have your function return the absolute path instead:

function get_abs_include_path($path) {
$path = str_replace(ROOT, DOC_ROOT, $path);
return $path;
}
...
include get_abs_include_path(ORDER . 'file.php');