This is sometimes a problem with includes when you're not using the absolute path on the system.
Explanation
Depending on how PHP is running could affect the way include
&require
work, if PHP is running from inside the appname directory it will work fine if php is told it's running inside the appname directory via a connector it's fine. however, if PHP is run for example www-data@/usr/bin/# php /var/www/somesite.com/htdocs/appname/index.php
the path can be broken.
Fix
if you use define("FS_ROOT", realpath(dirname(__FILE__)));
as the first thing other than if ther is a namespace inside index.php you can then use include FS_ROOT."/includes/config.php";
this means file paths are used from the root of the system so it gets evaluated to include "/var/www/somesite.com/htdocs/appname/index.php"
Why this differs
This differs from ROOT_PATH as ROOT_PATH is sometimes set by PHP configuration by web hosts and this could be the problem. as the PHP execution path could be wrong casing the PHP host application to look in the wrong place for includes and requries.
This also means no include
or require
should ever be using ../
as you should know the path from your codebase.
your appname/index.php
<?php define("FS_ROOT", realpath(dirname(__FILE__)));
include_once FS_ROOT.'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>
your appname/content/test.php
<?php
# as this file is loaded directly and is not in the root directory
# we apend the dirname result with ../ so realpath can resolve the root directory for this site
define("FS_ROOT", realpath(dirname(__FILE__)."../"));
include_once FS_ROOT.'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>
Ideally, you should go through a bootstrap and .htaccess so you don't have to change redefine the FS_ROOT
in every file loaded.
you can do this by making sure mod_rewrite
is enabled in apache
create file .htaccess
in appname folder
RewriteCond %{REQUEST_URI} .(php)$
RewriteRule .* bootstap.php [L]
create bootstrap.php
define("FS_ROOT", realpath(dirname(__FILE__)));
include_once FS_ROOT.'includes/config.php';
if(file_exists(FS_ROOT.$_SERVER['REQUEST_URI']){
include(FS_ROOT.$_SERVER['REQUEST_URI']);
}else{
// 404
}
this means you don't require the include for the config as it's automaticly included before the script for that request is wanted this is just a base outline and is not secure (and could be easily exploited to reveal system files contents) I would highly recommend reading up on how to use MVC's and how they work it will give you a better understanding of loading files on demand and requiring files.