vlucas / phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
BSD 3-Clause "New" or "Revised" License
13.11k stars 623 forks source link

Undefined index: USERNAME #523

Closed robineero closed 2 years ago

robineero commented 2 years ago

I am having issues with .env index names. USER and USERNAME can not be used as index names? For some reason only these variables are not loaded from the .env and are therefore undefined.

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$username = $_ENV['USERNAME'];

This results in PHP Notice: Undefined index: USERNAME but works well with USER_NAME and everything else.

benjaminhaeberli commented 2 years ago

Hi @robineero ! It's probably the same issue as the #137 : USER and USERNAME are probably already defined by another process. Try to write echo getenv('USERNAME') before those 3 lines and look at the result.

To solve this, you have two choices :

Cheers ✌️

GrahamCampbell commented 2 years ago

Benjamin is correct. You likely already have those set, so mutable mode is skipping them. If you use $_SERVER instead if $_ENV, you should be able to see it. In any case, mutable mode will be what you want, if you need to override something already set in $_SERVER.