Číst česky →

Drupal and the putenv() problem

1 min read Petr Pomajbík WebHosting PHP

Why is the putenv() function disabled?

The putenv() function changes the environment of the PHP process, which can be abused to bypass security restrictions or change the behaviour of the PHP runtime. On production environments such as shared servers, this is a risk for other websites on the same server.

✅ How to work around it:

Drupal (and libraries such as Symfony) typically use putenv() to load variables from .env files. This is easy to work around:

1. Don't use an .env file – set the values you need directly in settings.php instead:

$_ENV['VARIABLE_NAME'] = 'value';
$_SERVER['VARIABLE_NAME'] = 'value';

2. If you use your own .env loading, replace it with loading the file manually without putenv():

$env = parse_ini_file(__DIR__ . '/../.env', false, INI_SCANNER_RAW);
if ($env === false) {
    throw new RuntimeException('Cannot read .env');
}
foreach ($env as $k => $v) {
    $_ENV[$k] = $v;
    $_SERVER[$k] = $v;
}

If the website still tries to call putenv(), some part of the application (e.g. a library that loads .env variables) is not configured correctly for our hosting. We recommend disabling or replacing these libraries by setting the variables manually in settings.php.