我有一个非常简单的PHP脚本,但在我的一生中,我无法理解为什么一行不能工作。
主要剧本:
<?php
include("/includes/processes.php");
?>
[...]
<?php
if(getUserLevel() == 3) {
?>
[...]processes.php是在正确的地方和所有这些。它应该定义getUserLevel()。下面是:
<?php
function getUserLevel() {
if(isset($_COOKIE["userlvl"]) && isset($_SESSION["userlvl"]) {
if($_COOKIE["userlvl"] == $_SESSION["userlvl"]) return $_SESSION["userlvl"];
else return 0;
}
else {
return 0;
}
}
function usernameIs($name) {
if($_COOKIE["username"] == $name && $_SESSION["username"] == $name) return true;
else return false;
}
?>因此,当我转到index.php (主脚本)时,它会给我两个警告和一个致命错误:
Warning: include(/includes/processes.php): failed to open stream: No such file or directory in /home/u164546666/public_html/index.php on line 2
Warning: include(): Failed opening '/includes/processes.php' for inclusion (include_path='.:/opt/php-5.5/pear') in /home/u164546666/public_html/index.php on line 2
Fatal error: Call to undefined function getUserLevel() in /home/u164546666/public_html/index.php on line 27(第2行是include()调用,第27行是调用getUserLevel())
很明显,我为什么会犯致命的错误--因为include()失败了--但是为什么呢?是服务器配置问题还是我写错了?
文件树:
index.php
/includes
/processes.php发布于 2014-08-19 17:18:10
您可能需要相对路径,并且缺少.。
<?php
include("./includes/processes.php");
?>发布于 2014-08-19 17:18:29
将包含更改为
include_once dirname(__FILE__) . "/includes/processes.php";但是,我会选择require,因为该文件包含重要的功能。
require与include相同,除非失败,否则还会产生致命的E_COMPILE_ERROR级别错误。换句话说,它将停止脚本,而只包含一个允许脚本继续运行的警告(E_WARNING)。
发布于 2014-08-19 17:18:55
问题在于您的包含声明。只要去掉它周围的括号就可以了:
include "/includes/processes.php";https://stackoverflow.com/questions/25389277
复制相似问题