jirinovo
jirinovo

Reputation: 2275

php - how to properly include config?

In A.php I include config.php and B.php:

include_once("path/to/config.php");
include_once("path/to/B.php");

B.php is generic script used by other scripts. I don't know if script, which included B.php, also included config.php so in B.php there is

include_once("path/to/config.php");

The problem is that in A.php I can read all the variables from config.php, but in B.php they aren't set. If I do print_r(get_included_files()) in B.php, I can see that config.php is included.

What is causing this? How can I properly include that config.php so it will be available in B.php (and other scripts included by A.php...)?

EDIT: added scripts content.

config.php:

<?php

$db_ip = "";
$db_login="";
$db_pass ="";
$db_port = 30050;
$db_name_hlstats = "";
$db_name_csgo = "";
$db_name_report = "";

$db_web_host = "";
$db_web_port = "3306";
$db_web_login = "";
$db_web_pass = "";
$db_web_name = ""; 

B.php:

<?php

    function GetServers()
    {
        include_once("/data/web/virtuals/93680/virtual/config/config.php");
        include_once("/data/web/virtuals/93680/virtual/scripts/getPDO.php");
        include_once("/data/web/virtuals/93680/virtual/scripts/PDOQuery.php");

        print_r(get_included_files()); // shows config.php in included files
        echo "servers.php | $db_ip:$db_port"; // variables show nothing

        $pdo = getPDOConnection($db_ip, $db_login, $db_pass, $db_name_csgo, $db_port);

        $query = "SELECT ...";
        $result = getPDOQueryResult($pdo, $query, __FILE__, __LINE__);
        $res = array();

        foreach ($result as $row)
        {
          $res[$row["server_id"]] = $row;
        }

        return $res;
    }

Upvotes: 2

Views: 2306

Answers (1)

Get Off My Lawn
Get Off My Lawn

Reputation: 36311

the reason you are not getting results in B.php is because you are using include_once which will only include the file if it has not already been included. In your case you include it in A.php so it sees that it has already been loaded and will not load it again within B.php (aka it will skip the include).

If you use include instead within your B.php function you should see your results.

require_once and include_once are usually best when including libraries, that contain classes/methods/functions so you don't accidentally attempt to define them more than once.

Examples


class MyClass{

    // Contains some methods    

}

When including the file doing this:

include 'MyClass.php';
include 'MyClass.php';

You will get an error saying '"MyClass" has already been defined' when it tries to load the second include.


When doing this:

include_once 'MyClass.php';
include_once 'MyClass.php';

PHP will skip loading the second include and you won't get an error saying that the class has already been defined.

So for you B.php it would be a good idea to do a require_once or include_once so you don't redefine your function and get an error.

You should also note that there are differences between include* and require*.

When using include* if the file couldn't be loaded the script will continue to run and depending on what you're doing could corrupt data/results.

When using require* if the file couldn't be loaded the script will end execution.

Upvotes: 2

Related Questions