Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this problem when using putenv/getenv function? #238

Closed
ghost opened this issue Jun 29, 2017 · 3 comments
Closed

Is this problem when using putenv/getenv function? #238

ghost opened this issue Jun 29, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 29, 2017

(sorry for my bad english)

I made a simple test. I have 2 sites in a same Apache server (using virtual host)
With XAMPP stack, apache 2.4, I setup virtual host for 2 folders: test1.com and test2.com

  • In test1.com: I put a php file (test1.com/putenvTest.php) with putenv('xxx=1'). After that using curl function to make a http GET request to test2.com/getenvTest.php
  • In test2.com: I put getenvTest.php with var_dump( getenv('xxx') );
  • Go to browser and hit test1.com/putenvTest.php => the value 1 is displayed
    So, when I call putenv('xxx=1') in one site. I can get that value in other site (over curl request).
    I think this is a problem in some frameworks using dotenv to store .env config such as Laravel.
    When I setup 2 Laravel projects (each project have separate folder, and separate .env file), one of them is WEB and other is API, WEB will call API to get data (using curl). Many keys in 2 .env files are the same. So, API will ignore keys was existed because WEB was load that keys => API will get wrong config (these configs are of WEB, not API)

Thanks

@sisve
Copy link

sisve commented Jul 19, 2017

This happens because Apache on Windows executes all php-requests within the same process, and the first request sets the variables and the second reads them. Dotenv will not overwrite existing variables, that is why the second request don't get all of the second sites .env stuff.

This is a well-known issue and you will have other issues with your setup too, like building localized sites that calls setlocale() which also is process-wide.

Regarding Laravel, you are supposed to run php artisan config:cache to build a cached configuration file. This means that values are read from .env and stored in a cached file, and Laravel will no longer call dotenv on every request.

Related: #76 (Changing process environment unsafe on multithreaded servers)

@GrahamCampbell
Copy link
Collaborator

Thanks, yeh. This is a known issue, with known solutions, the solutions being, don't read the env variables directly, load them once into config.

@GrahamCampbell
Copy link
Collaborator

The current version of phpdotenv is robust and thread-safe. The following example will work in a multithreaded environment. This avoids using the adapter that would have called putenv and getenv, which are not threadsafe.

<?php

use Dotenv\Environment\Adapter\EnvConstAdapter;
use Dotenv\Environment\Adapter\ServerConstAdapter;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Dotenv;

$factory = new DotenvFactory([new EnvConstAdapter(), new ServerConstAdapter()]);

Dotenv::create($path, null, $factory)->load();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants