文件包含在根路径中时函数停止工作(引导斜杠) [英] Functions stop working when file included with root path (leading slash)

查看:116
本文介绍了文件包含在根路径中时函数停止工作(引导斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP文件在我的根目录INCLUDE header.php中。 Header.php INCLUDEs functions.php。我在一个子目录中添加了新页面,所以我在header.php中添加了所有链接的前导斜线:CSS,菜单项和随后的INCLUDE到functions.php。 CSS和菜单项在子目录中的这个页面上正常工作,但是这些功能不起作用。



的组合是否包含并且引导斜线需要修改功能



从根目录中的页面:

 包括( '的header.php'); 

从子目录中的页面:

 包括( '/ header.php文件'); 

来自header.php:

 包括( '/的functions.php'); 

不再有效的函数(从根目录或子目录中的页面调用) p>

 函数show_date($ array_name){
if(date(YF j,strtotime($ array_name [exhibit_open ]))== date(YF j,strtotime($ array_name [exhibit_close]))){
echo date(F j,Y,strtotime($ array_name [exhibit_open])) ;
}
elseif(date(Y,strtotime($ array_name [exhibit_open]))!= date(Y,strtotime($ array_name [exhibit_close]))){
$ first_date_format =F j,Y;
echo date($ first_date_format,strtotime($ array_name [exhibit_open]))。 - 。 date(F j,Y,strtotime($ array_name [exhibit_close]));
} elseif(F,strtotime($ array_name [exhibit_open]))!= date(F,strtotime($ array_name [exhibit_close]))){
$ first_date_format =F j;
echo date($ first_date_format,strtotime($ array_name [exhibit_open]))。 - 。 date(F j,Y,strtotime($ array_name [exhibit_close]));
} else {
$ first_date_format =j;
echo date(F j,strtotime($ array_name [exhibit_open]))。 - 。日期($ first_date_format,的strtotime($ ARRAY_NAME [ exhibit_close]))。 ,。日期( Y,的strtotime($ ARRAY_NAME [ exhibit_close]));
}

}

解决方案

只要你知道,如果你打算让你请求的PHP页面也自己请求其他页面,使用 require_once可能是有益的而不是 include 。这将使它包含的页面不再重复,而且你不必担心不小心包含多次的内容。



这就是说。 。当你在根目录中请求一个页面时,它会在根目录中请求header.php,然后在根目录中请求functions.php。但是,如果您从子目录请求, ../ header.php 将引用根目录中的header.php,但整个文件将包含在内,然后它的PHP子页面中的页面最终试图包含 /functions.php 。它需要请求 ../ functions.php ,但这会导致根目录中的所有内容停止工作。



我建议在header.php中设置一个变量,沿着 $ root = $ _SERVER ['DOCUMENT_ROOT']; 的行写入所有包含在header.php应该像 include($ root。/ functions.php);



$ _SERVER ['DOCUMENT_ROOT'] 会让你的目标网址到根目录,这将使你确保你引用正确的地方,无论你从哪里请求header.php。


My PHP files in my root directory INCLUDE header.php. Header.php INCLUDEs functions.php. I'm adding new pages in a subdirectory, so I added leading slashes to all my links in header.php: CSS, menu items and the subsequent INCLUDE to functions.php. The CSS and menu items work fine on this page in the subdirectory, but the functions don't work. There are no links in the functions that seem to need leading slashes.

Does the combination of include and leading slashes require modifying functions?

From a page in the root directory:

include('header.php');

From a page in the subdirectory:

include('/header.php');

From header.php:

include('/functions.php');

And the function that no longer works (called from pages in the root directory or subdirectory):

function show_date($array_name){
if (date("Y F j",strtotime($array_name["exhibit_open"])) == date("Y F j",strtotime($array_name["exhibit_close"]))){
    echo date("F j, Y",strtotime($array_name["exhibit_open"]));
}
elseif (date("Y",strtotime($array_name["exhibit_open"])) != date("Y",strtotime($array_name["exhibit_close"]))) {
    $first_date_format = "F j, Y";
    echo date($first_date_format,strtotime($array_name["exhibit_open"])). " - ". date("F j, Y",strtotime($array_name["exhibit_close"]));
} elseif (date("F",strtotime($array_name["exhibit_open"])) != date("F",strtotime($array_name["exhibit_close"]))){
    $first_date_format = "F j";
    echo date($first_date_format,strtotime($array_name["exhibit_open"])). " - ". date("F j, Y",strtotime($array_name["exhibit_close"]));
} else {
    $first_date_format = "j";
    echo date("F j",strtotime($array_name["exhibit_open"])). " - ". date($first_date_format,strtotime($array_name["exhibit_close"])). ", ". date("Y",strtotime($array_name["exhibit_close"]));
}

}

解决方案

Just so you're aware, if you're going to have the php pages you're requesting also request other pages themselves, it may be beneficial to use require_once instead of include. That will make it so none of the pages that are included repeat themselves and you don't have to worry about accidentally including something more than once.

That being said... when you request a page in the root directory, it will request header.php in the root directory which will in turn request functions.php in the root directory. However, if you request from the subdirectory, ../header.php will reference header.php in the root directory, but that whole file will get included, and then its the php page in the subdirectory that ends up trying to include /functions.php. It would need to request ../functions.php, but that would cause everything in the root directory to stop working.

I'd suggest setting a variable in header.php along the lines of $root = $_SERVER['DOCUMENT_ROOT']; Then, all includes in header.php should be like include($root."/functions.php");

$_SERVER['DOCUMENT_ROOT'] will get you objective url to the root, which will enable you to make sure you're referencing the correct place no matter where you're requesting header.php from.

这篇关于文件包含在根路径中时函数停止工作(引导斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆