You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When developing and deploying your applications you are interacting with two different environments. These two places both execute your code but will do so using different credentials, such as database connection credentials, for example. How do you tackle these differing credentials? dotEnv will solve this issue by allowing you to configure your environments and easily switch between them.
8
+
A `.env` file parsing and loading library for PHP.
9
+
10
+
-[What is it and why should I use it?](#what-is-it-and-why-should-i-use-it)
@@ -21,7 +23,7 @@ Run `composer require josegonzalez/dotenv:dev-master`
21
23
22
24
Or add the plugin to your project's `composer.json` - something like this:
23
25
24
-
```composer
26
+
```javascript
25
27
{
26
28
"require": {
27
29
"josegonzalez/dotenv":"dev-master"
@@ -335,6 +337,44 @@ $expect('FOO'); // Returns false if `env` is missing FOO
335
337
?>
336
338
```
337
339
340
+
## What is it and why should I use it?
341
+
342
+
When developing and deploying your applications you are typically interacting with various environments - production and development for instance. These environments both execute your code, but will do so using different credentials. You may also wish to distribute your application to developers without accidentally giving them access to important external services.
343
+
344
+
Simple examples include authentication keys to your email provider or database connection credentials. You would never want to accidentally send testing emails to all your users, or run a `DROP TABLE` statement against production because you ran your unit tests.
345
+
346
+
How do you tackle these differing credentials? The `php-dotenv` helps solve this issue by allowing you to configure your environments in a universal fashion, making it easy to safely switch between environments, as well as share those environments with multiple projects/languages.
347
+
348
+
Need more reasons? Check out the [twelve-factor app docs on configuration](http://12factor.net/config).
349
+
350
+
## Rules to follow
351
+
352
+
When using `php-dotenv`, you should strive to follow the following rules:
353
+
354
+
- Add your `.env` file to a gitignore and use a `.env.default` or `.env.example` to set defaults for your projects. This allows your development team to override defaults in a method that works for their local environment.
355
+
- Always set sane development defaults for any credential. If necessary, disable features when those credentials are "invalid".
356
+
- Where necessary, add comments to credentials with information as to what they are, how they are used, and how one might procure new ones.
357
+
- As `php-dotenv` uses more lax procedures for defining environment variables, ensure your `.env` files are compatible with your shell. A good way to test this is to run the following:
358
+
359
+
```shell
360
+
# source in your .env file
361
+
source .env
362
+
# check the environment
363
+
env
364
+
```
365
+
- Avoid running `php-dotenv`in production settings, and instead set environment variables in your webserver, process manager, or in bash before running your commands. A simple way to ensure this is to check forthe existence of a sentinel environment variable that is *only* setin production:
366
+
367
+
```php
368
+
// APP_NAME isn't set in staging/dev
369
+
if (!env('APP_NAME')) {
370
+
$loader = new josegonzalez\Dotenv\Loader([
371
+
__DIR__ '/.env',
372
+
__DIR__ '/.env.default'
373
+
]);
374
+
$loader->parse()->toEnv();
375
+
}
376
+
```
377
+
338
378
## General Security Information
339
379
340
380
If you configure `php-dotenv` to output configuration in any of the ways listed above and then dump them, they may be available to undesired users. For instance, using a project like [filp/whoops](https://github.com/filp/whoops) in conjunction with `$Loader->toServer()` can result in outputting sensitive data to your users if you leave whoops enabled in production.
@@ -343,6 +383,7 @@ For this reason, `php-dotenv` never populates data to an environment variable by
343
383
344
384
Many error reporting tools have the option of whitelisting or blacklisting sensitive data, and you should familiarize yourself with said tooling.
0 commit comments