Question:
Can anyone help me wirh php include?
Henrique Da Cruz
2012-12-10 18:03:47 UTC
Ok, here's the deal, i am creating a site which is located on a free host's server. I am trying to include a file (located on a different directory) via, the include function. I got a "URL file-access is disabled in the server configuration in"... error after using an absolute path. My question is the following: I know that there is a way to get around the server configuration that turns off the absolute path including. I used it before on files with the same host, but the server i was in was wiped clean and i lost the templates i had made. Please help me include files using an absolute, rather than a relative, path. I find the relative paths somewhat confusing, as it gets tricky when you include a file within an included file. Please help me out, i have already searched online,but found no real help. I have no more hair to pull out. Thanks in advance for the help.
Three answers:
VBA
2012-12-10 21:17:11 UTC
Use relative paths. I know what you mean, about the trickiness of included files within included files, but there's an easy solution to that:



__FILE__ is a "magic constant" that always contains the path and name of the file it's in, no matter how deep in the include chain it appears.



__DIR__ is a "magic constant" that always contains the directory of the file it's in, no matter how deep in the include chain it appears.



So if c.php includes b.php, and b.php include a.php, and a.php contains echo __FILE__, you'll see a.php's full file name.



By the way, I believe what you're for is allow_url_fopen, but I highly recommend using other solutions than turning it on. It's turned off by default for a reason (to prevent cross-site scripting attacks), so don't turn it on unless you really know what you're doing!



Either use relative includes or use $_SERVER['DOCUMENT_ROOT'].
Ash
2012-12-12 13:46:29 UTC
Valentina is right on all accounts, but it sounds like the best option for you is the last one noted in her response which is using $_SERVER['DOCUMENT_ROOT'].



The code you'd be writing would look something like this:



include $_SERVER['DOCUMENT_ROOT'] . '/includes/header.php';



Also, there's a 3rd option (4th if you consider turning allow_url_fopen ON an option, but I don't). Although I'm not privy to using this function for this purpose and I can see why Valentina omitted it, you can use file_get_contents(). If you echo the code using file_get_contents, you can use the various functions in those includes, but this doesn't always come out as planned. Anyway, you're allowed to use absolute paths in file_get_contents(), but it might be wise to just come up with a function above all your includes and use it to make the correct relative path.



Here's the function I wrote:



function myInclude($absolute_path){

// Check HTTP_HOST

if (isset($_SERVER['HTTP_HOST'])){

$_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']);



if(!preg_match('/^\[?(?:[a-z0-9-:\]_]+\.?)+$/', $_SERVER['HTTP_HOST'])){

header('HTTP/1.1 400 Bad Request');

exit;

}

} else { $_SERVER['HTTP_HOST'] = ''; }



$target_array = explode('/', $absolute_path);

$host = explode('/', $_SERVER['HTTP_HOST']);

$current_directory_array = explode('/', $_SERVER['SCRIPT_NAME']);

array_shift($current_directory_array); // remove empty item



// chop the base url & host name off the absolute path

for($i = 0; $i < count($target_array); $i++){

if($target_array[$i] == $_SERVER['HTTP_HOST']){

$target_path_array = array_slice($target_array, -(count($target_array)-($i+1)));

}

}



if(!$target_path_array){ $target_path_array = $target_array; }



// pop off all the same directories

$length = (count($current_directory_array) > count($target_path_array) ? count($current_directory_array) : count($target_path_array));

for($i = 0; $i < $length; $i++){

if($current_directory_array[$i] == $target_path_array[$i]){

array_shift($current_directory_array);

array_shift($target_path_array);

} else {

array_shift($current_directory_array);

array_shift($target_path_array);

break;

}

}



// any remaining parts of the current scripts path, change to ../

for($i = 1; $i < count($current_directory_array); $i++){ $relative_path .= '../'; }



$relative_path .= implode('/', $target_path_array);



return include($relative_path);

}



So long as you put this function above any of your includes on the landing page, it will work on any nested included file... assuming you don't visit the included file directly.



Also, keep in mind that the HTTP_HOST variable is user input (which is why it's checked), so if this is changed then the function might fail.



However, the function does allow you to omit the host entirely.
puccinelli
2016-10-17 14:20:58 UTC
...or you could The '../' (dot dot curb, no areas) brings you up one itemizing (to the determine itemizing) from the subdirectory the place you're at present working your record from. on occasion i like doing this extra beneficial than which includes an entire direction because of the fact if I circulate the main well-known itemizing up or down a point the code would not could substitute.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...