Walter Nascimento
Posted on September 11, 2022
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());
π‘ NOTE: More information in the documentation
to create a new value we just need to use putenv()
;
<?php
putenv("TEST=test");
and to display the value we use
<?php
echo getenv("TEST");
π‘ 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'];
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
and inside put the values you want, for this example I will just put two variables
APP_NAME=Project
APP_ENV=local
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;
}
}
}
and now just use
<?php
require __DIR__ . '/../vendor/autoload.php';
(new App\Controller\DotEnvEnvironment)->load(__DIR__ . '/../');
echo getenv('APP_ENV');
echo $_ENV['APP_NAME'];
.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
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
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
Now we create the .env
file
APP_NAME=Project
APP_ENV=local
Now we load the file
<?php
require __DIR__ . '/../vendor/autoload.php';
Dotenv\Dotenv::createUnsafeImmutable(__DIR__ . '/../')->load();
echo getenv('APP_ENV');
echo $_ENV['APP_NAME'];
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
Posted on September 11, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.