Access to global variables in a separate PHP script?

I am trying to import some variables from a PHP script. It seems simple, but I can't get it to work.

The script contains some global variables:

$server_hostname = "localhost";
$server_database = "kimai";
$server_username = "root";
$server_password = "";
$server_conn     = "mysql";
$server_type     = "";
$server_prefix   = "kimai_";
$language        = "en";
$password_salt   = "7c0wFhYHHnK5hJsNI9Coo";

Then in my script, I would like to access these variables, so I did:

require_once 'includes/autoconf.php';   
var_dump($server_hostname);

But it just outputs NULL. I also tried:

require_once 'includes/autoconf.php';

global $server_hostname;    
var_dump($server_hostname);

but still not working.

I have added several statements echoto the "autoconf.php" file, so I know that it is loading.

How can I access these variables?

+5
source share
6 answers

, - , , require_once, . require .

+2

:

global $server_hostname;
$server_hostname = "localhost";
+2

, .

require_once 'includes/autoconf.php';   

, autoconf.php

if (file_exists('includes/autoconf.php')) require_once 'includes/autoconf.php';
else echo 'File not exists';

.

+1

?

( "server_hostname", "" ); ( "server_hostname", "" );

0

, , /,

php.ini display_errors = On E_ALL, , .

0

, .

<? //PHP 5.4+
\call_user_func(static function(){
    $globals = \get_defined_vars();
    include 'includes/autoconf.php';
    $newVars = \array_diff_key($globals, \get_defined_vars());
    foreach($newVars as $name => $value){
        \define($name, $value);
    }
});
//Variables defined in file are now constants!
?>
0

All Articles