I have a problem with php include section. In order to fully explain the problem, I created a test page for you.
First I want to show the schema of the files to you. Also you can download the test files from this LINK and you can test it online TEST LINK
As you can see, there is a subfolder in the htdocs
(root) file and all php files in there. Now I'll show you the php code within the file, respectively.
appname/index.php
<?php include_once 'includes/config.php';?>
<div class="callPage">Click Here</div>
<div id="testBox"></div>
<script type="text/javascript">
$(document).ready(function(){
var siteurl = '<?php echo $url;?>';
$("body").on("click",".callPage", function(){
$.ajax({
url: siteurl+'content/test',
beforeSend: function() {
//Do something
},
complete:function(){
//Do something
},
success:function(response){
$("#testBox").html(response);
}
});
});
function LoadPage(){
$.get(siteurl+'content/test', function(data) {
$('#testBox').html(data);
});
}
LoadPage();
});
</script>
appname/content/test.php
<?php
include_once 'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>
appname/includes/config.php
<?php
$url = 'http://localhost:8888/';
$text = 'Well Come! How are you today ?';
$worked = 'It is working :)';
?>
When you open the TEST LINK, LoadPage();
javascript function will call test.php
in the content file and display it in #testBox
. First you will not see anything in #testBox
from index.php
. Because config.php
can not be included from test.php
.
I know if I change this line include_once 'includes/config.php';
from test.php
like this include_once '/appname/includes/config.php';
then problem will be fix.
But if the pages multiply and, I want to use the files in the root (htdocs or www) folder, I need to delete appname (subfolder name) => include_once 'appname/includes/config.php';
from all files. It will be a big problem when these files multiply.
Actually the question is exactly:
See Question&Answers more detail:osHow can we include php files without specifying the full path to the include, when the application's path relative to the
DOCUMENT_ROOT
is variable or unknown andinclude_path
cannot be reliably modified by all application users?