0

I have cloned PHP code from a Linux server onto a Windows WAMP server. When I run the index.php page the error in the picture appears. From what I can work out in my Windows WAMP server the environment variables have not been initialized.

How do I initialize these variables in the environment?

Thank you.

DB::init([
    'host'              => $_ENV['DB_HOST'],
    'database'          => $_ENV['DB_DATABASE'],
    'user'              => $_ENV['DB_USERNAME'],
    'password'          => $_ENV['DB_PASSWORD'],
]);

https://i.stack.imgur.com/tCxwT.png

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
franke
  • 3
  • 1
  • 3
  • 1
    Does this answer your question? [How to set global environment variables for PHP](https://stackoverflow.com/questions/19696230/how-to-set-global-environment-variables-for-php) – BadHorsie Sep 21 '20 at 13:21
  • I have a file in the directory:C:\WAMP\bin\apache\apache2.4.41\conf called httpd. I have entered into it: SetEnv DB_DATABASE 'pettbot' I entered this text in a random place and it is not inside any tags as I'm not sure what the tags would be. It still does not work. Have I neglected to put appropriate tags around it? Am I using the right file? Is my syntax correct? – franke Sep 21 '20 at 13:27
  • It doesn't need to be within any tags. Did you restart Apache? – BadHorsie Sep 21 '20 at 13:31
  • I have restarted Apache and the problems remains. – franke Sep 21 '20 at 13:48
  • Does the value come through if you use `getenv('DB_DATABASE')` in the PHP? – BadHorsie Sep 21 '20 at 14:04
  • Yes. Why is there a difference between $_ENV[ ] and getenv()? – franke Sep 21 '20 at 14:33

1 Answers1

-1

The DB initialization method is looking for the host, database, user and password to be defined somewhere.

I'd recommend setting them in a config file that is ignored by any SVN (git, etc). You'd end up with a file that looks like this:

$_ENV['DB_HOST'] = //name_of_your_host;
$_ENV['DB_DATABASE'] = //name_of_database_to_start_in;
$_ENV['DB_USER'] = //name_of_mysql_user_to_access_database_above;
$_ENV['DB_PASSWORD'] = //password_of_user_above;

Fill in the commented parts, and include this before you call your DB::init. One very, very, very important part to remember is do not include the config file in your git or other repository!! Otherwise, if your repository is compromised, the attacker will have access to both a valid DB_USER and DB_PASSWORD!!