PHP包括调用throws警告但仍在继续

时间:2014-08-19 17:14:38

标签: php include

我有一个非常简单的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

3 个答案:

答案 0 :(得分:4)

您可能需要相对路径并且缺少.

<?php
    include("./includes/processes.php");
?>

答案 1 :(得分:1)

将您的包更改为

include_once dirname(__FILE__) . "/includes/processes.php";

但是,我会选择require,因为该文件包含重要功能。

  

require与include相同,除非失败,它还会产生致命的E_COMPILE_ERROR级别错误。换句话说,它将暂停脚本,而include仅发出警告(E_WARNING),允许脚本继续。

答案 2 :(得分:-2)

问题在于您的include语句。只需摆脱围绕它的括号:

include "/includes/processes.php";