【发布时间】:2018-05-27 08:12:24
【问题描述】:
我的 php 包含部分有问题。为了充分解释问题,我为您创建了一个测试页面。
首先,我想向您展示文件的架构。您也可以从这个LINK下载测试文件并在线测试TEST LINK
如您所见,htdocs(根)文件中有一个子文件夹,所有的 php 文件都在其中。现在我将分别向您展示文件中的php代码。
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>
应用名称/内容/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 :)';
?>
当您打开TEST LINK时,LoadPage(); javascript函数会调用内容文件中的test.php,并显示在#testBox中。首先,您不会在 #testBox 中看到来自 index.php 的任何内容。因为config.php 不能包含在test.php 中。
我知道如果我将这一行 include_once 'includes/config.php'; 从 test.php 更改为这样的 include_once '/appname/includes/config.php';,那么问题将得到解决。
但是如果页面增加,并且我想使用根(htdocs 或 www)文件夹中的文件,我需要从所有文件中删除 appname(子文件夹名称)=> include_once 'appname/includes/config.php';。当这些文件大量增加时,这将是一个大问题。
其实问题就是:
当应用程序相对于
DOCUMENT_ROOT的路径是可变的或未知的并且include_path不能被所有应用程序用户可靠地修改时,我们如何在不指定包含的完整路径的情况下包含php 文件?
【问题讨论】:
-
define('ROOT_PATH', dirname(DIR) . '/inc/inc.php'); 真的不包含任何文件.剩下的问题还不清楚。
-
@panther Normaly 包括 inc.php 文件但在 LoadPage() 之后;不包括
-
@DevStud 你到底想用这个定义做什么?
-
@madalinivascu 我想在 ajax
LoadPage();之后将inc.php文件包含在test.php中 -
使用 Ajax 加载方法而不是 php 包含。 jquery-tutorial.net/ajax/the-load-method
标签: javascript php jquery ajax .htaccess