ArtOfCode
ArtOfCode

Reputation: 5712

PHP include call throws warnings but continues

I've got a pretty simple PHP script, but I can't for the life of me work out why one line doesn't work.

The main script:

<?php
    include("/includes/processes.php");
?>
[...]
<?php
    if(getUserLevel() == 3) {
        ?>
[...]

processes.php is in the right place and all that. It should be defining getUserLevel(). Here it is:

<?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;
}

?>

So when I go to index.php (the main script), it gives me two warnings and one fatal error:

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

(line 2 is the include() call, line 27 the call to getUserLevel())

It's pretty obvious why I've got the fatal error - because the include() has failed - but why is that? Is it a server config issue or have I just written it wrong?

The file tree:

index.php
/includes
    /processes.php

Upvotes: 1

Views: 1352

Answers (3)

TalwinderSingh
TalwinderSingh

Reputation: 193

The issue is with your include statement. Just get rid of the parenthesis around that as:

include "/includes/processes.php";

Upvotes: -2

ʰᵈˑ
ʰᵈˑ

Reputation: 11365

Change your include to

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

However, I'd go for require as the file includes vital functionality.

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

Upvotes: 1

jh314
jh314

Reputation: 27802

You are probably need the relative path and is missing the .

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

Upvotes: 4

Related Questions