DEV Community

Cover image for DOTENV in PHP
Walter Nascimento
Walter Nascimento

Posted on • Updated on

DOTENV in PHP

When we are creating a project, there are always some sensitive values, and at the beginning we don't know where to write it and with that we end up writing it in my code, such as database passwords and others.

But this is a big problem, firstly in terms of security because your data is visible to everyone with access to the code and secondly, it gets complicated whenever you change environments such as production and so on.

And to solve this came .env, from which the point(dot) for linux environment is to leave files hidden, and the name env is an abbreviation for environment.

To understand better, let's see it in practice.

CODE

First let's check the values that are in the environment variables and for that we can use getenv();

<?php
print_r(getenv());
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ NOTE: More information in the documentation

to create a new value we just need to use putenv();

<?php
putenv("TEST=test");
Enter fullscreen mode Exit fullscreen mode

and to display the value we use

<?php
echo getenv("TEST");
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ NOTE: It is a good practice to have environment variable names in uppercase

In addition to getenv we also have $_ENV

$_ENV is an associative array where each of its keys is an environment variable pointing to its value. The problem is that it might not load depending on the php.ini configuration. To enable it, put the β€œE” option in the β€œvariables_order” directive, where each letter represents a superglobal to be loaded.

And to display the value we use

<?php
echo $_ENV['APP_ENV'];
Enter fullscreen mode Exit fullscreen mode

Now that we know where the variables are, how to create more and how to call just one, let's make php read the values from an .env file and save it in the environment variables inside php.

DOTENV

First create the .env file.

touch .env
Enter fullscreen mode Exit fullscreen mode

and inside put the values you want, for this example I will just put two variables

APP_NAME=Project
APP_ENV=local
Enter fullscreen mode Exit fullscreen mode

PHP CLASS

First let's create the class

<?php

namespace App\Controller;

class DotEnvEnvironment
{

   public function load($path): void
   {
       $lines = file($path . '/.env');
       foreach ($lines as $line) {
           [$key, $value] = explode('=', $line, 2);
           $key = trim($key);
           $value = trim($value);

           putenv(sprintf('%s=%s', $key, $value));
           $_ENV[$key] = $value;
           $_SERVER[$key] = $value;
       }
   }
}
Enter fullscreen mode Exit fullscreen mode

and now just use

<?php

require __DIR__ . '/../vendor/autoload.php';

(new App\Controller\DotEnvEnvironment)->load(__DIR__ . '/../');

echo getenv('APP_ENV');
echo $_ENV['APP_NAME'];

Enter fullscreen mode Exit fullscreen mode

.gitignore

Never send the .env to the server from the code, because the purpose of the .env is to separate sensitive information from the code so it doesn't matter if you still send it to the server, and to avoid this just add the value to the .gitignore file.

.env
Enter fullscreen mode Exit fullscreen mode

If you are developing as a team, you can include the .env.example file and add some fictitious values there so that those who use the project can understand what should be done example.

DATABASE=database-name
PASSWORD=your-password-here
Enter fullscreen mode Exit fullscreen mode

TIP

You can use different .env files per environment, so you can have .env.staging for staging, .env.production for production, .env.testing for testing, etc.

Library

Despite having shown how to create a file that loads .env, I don't think it's the best solution, as there are already libraries ready, more robust and much more tested, so if you're just curious to understand how it works, go ahead and create your own, but if you want to create or use it in a professional project I recommend the vlucas/phpdotenv library, so excellent that the laravel framework itself uses and therefore I will show you how to use it.

πŸ’‘ NOTE: symfony/dotenv is used in Symfony framework and sometimes i use it too

First add it to composer

composer require vlucas/phpdotenv
Enter fullscreen mode Exit fullscreen mode

Now we create the .env file

APP_NAME=Project
APP_ENV=local
Enter fullscreen mode Exit fullscreen mode

Now we load the file

<?php

require __DIR__ . '/../vendor/autoload.php';

Dotenv\Dotenv::createUnsafeImmutable(__DIR__ . '/../')->load();

echo getenv('APP_ENV');
echo $_ENV['APP_NAME'];

Enter fullscreen mode Exit fullscreen mode

That's it, simple as that, with just a few lines you already have the project configured.


Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!
😊😊 See you! 😊😊


Support Me

Youtube - WalterNascimentoBarroso
Github - WalterNascimentoBarroso
Codepen - WalterNascimentoBarroso

Top comments (0)