Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 11, 2022 05:35 pm GMT

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();

<?phpprint_r(getenv());

NOTE: More information in the documentation

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

<?phpputenv("TEST=test");

and to display the value we use

<?phpecho 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

<?phpecho $_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=ProjectAPP_ENV=local

PHP CLASS

First let's create the class

<?phpnamespace 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

<?phprequire __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-namePASSWORD=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.

First add it to composer

composer require vlucas/phpdotenv

Now we create the .env file

APP_NAME=ProjectAPP_ENV=local

Now we load the file

<?phprequire __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


Original Link: https://dev.to/walternascimentobarroso/dotenv-in-php-45mn

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To