Load PHP environment variables from .env (INI syntax) file only on $_ENV
and runtime.
composer require melbahja/environ
Some env libraries load variables into $_SERVER
and $_REQUEST
, which is a stupid idea that can lead to expose credentials and insert sensitive information into log files. environ
only load variables to superglobal $_ENV
and runtime via putenv
.
/path/to/your/project/.env
; set a var
APP_MODE = "dev"
; array
[DATABASE]
HOST = '127.0.0.1'
USERNAME = 'root'
PASSWORD = null
YourScript.php
require 'vendor/autoload.php';
use Melbahja\Environ\Environ;
// environ looking for .env or env.ini file in your directory
Environ::load('/path/to/your/project');
var_dump(Environ::get('APP_MODE')); // string
var_dump(Environ::get('DATABASE')); // array
var_dump($_ENV['DATABASE']); // array
Arrays will not be available in getenv()
, you can only access them via $_ENV
or Environ::get()
.
# if you want a helper
function env(string $var, $default = null)
{
return \Melbahja\Environ\Environ::get($var, $default);
}
Environ::load(string $directory): bool
Environ::get(string $var, $default = null): mixed
Environ::set(string $var, $value): bool
# Example: Environ::is('apache'), Environ::is('cli')
Environ::is(string $sapi): bool
MIT Copyright (c) 2018-present Mohamed Elbahja