PHP getenv always returns false

前端 未结 2 997
死守一世寂寞
死守一世寂寞 2021-01-29 05:21

The getenv() always returns false. I am using Symfony dotenv library and loading my variables from a .env file in the root of my project.



        
2条回答
  •  无人共我
    2021-01-29 06:00

    I m not using symfony but i've encountered the some problem i am using the vlucas library this is my first code that caused the problem

    define('BASE_PATH',realpath(__DIR__.'/../../'));
    require_once __DIR__.'/../../vendor/autoload.php';
    $dotEnv = Dotenv\Dotenv::createImmutable(BASE_PATH);
    $dotEnv->load();
    $appName=$_ENV['APP_NAME'];
    $appName2=getenv('APP_NAME'];
    
    var_dump($appName) // return "This is my website";
    var_dump($appName2) // return false;
    

    i didn't knwo the problem at first but it seems that it was because putenv() and getenv() are not thread safe

    so i changed it to this code

    define('BASE_PATH',realpath(__DIR__.'/../../'));
    require_once __DIR__.'/../../vendor/autoload.php';
    $dotEnv = Dotenv\Dotenv::createUnsafeImmutable(BASE_PATH);// <======== :) look here
    $dotEnv->load();
    $appName=$_ENV['APP_NAME'];
    $appName2=getenv('APP_NAME'];
    
    var_dump($appName) // return "This is my website";
    var_dump($appName2) // return "This is my website";
    

    i hope this resolves your issue

提交回复
热议问题