PHP dotenv loads environment variables from .env
to getenv()
, $_ENV
and $_SERVER
automagically.
You should never store sensitive credentials in your code. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.
Add your application configuration to a .env file in the root of your project. Make sure the .env file is added to your .gitignore so it is not being checked-in.
DEFINE
1 2 3 4 |
S3_BUCKET="dotenv" SECRET_KEY="souper_seekret_key" |
LOAD
1 2 3 4 5 6 7 8 9 |
$dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); // OR WITH filename $dotenv = new Dotenv\Dotenv(__DIR__, 'myconfig'); $dotenv->load(); |
ACCESS
1 2 3 4 5 |
$s3_bucket = getenv('S3_BUCKET'); $s3_bucket = $_ENV['S3_BUCKET']; $s3_bucket = $_SERVER['S3_BUCKET']; |