Skip to content

A basic function to load variables from a .env file.

Notifications You must be signed in to change notification settings

codeadamca/php-env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hiding PHP Environment Varaibles

A basic vanilla PHP function to load variables from an .env file.

When you are creating a basic vanilla PHP project and want to keep your database credentials and/or API key out of your GitHub repository you can use a .env file.

  1. Create a file called .env and place it in your project folder.

  2. Put your actual database credentials and API keys in your `.env``file:

    DB_HOST=<DB_HOST>
    DB_DATABASE=<DB_DATABASE>
    DB_USERNAME=<DB_USERNAME>
    DB_PASSWORD=<DB_PASSWORD>
    
    API_SECRET=real_api_key
  3. Create a second copy of your .env file and name it .env.sample. Use this file to provide instructions to the next programmer using your project code:

    DB_HOST=localhost
    DB_DATABASE=database
    DB_USERNAME=username
    DB_PASSWORD=password
    
    API_SECRET=api_key
  4. Make sure your GitHub repo does not include the file named .env. In your .gitignore file add:

    .env
    

Note

I have excluded this step in this repo so you can view both the .env and .env.sample files.

  1. If your .env file is accessible from a browser, use a .htaccess file to hide your .env file:

    <Files .env>
    order allow,deny
    Deny from all
    </Files>
  2. In your database connection file, add this function to load the variables from the .env file, and place them in matching constants:

    $env = file(__DIR__.'/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    foreach($env as $value)
    {
      $value = explode('=', $value);
      define($value[0], $value[1]);
    }
  3. Finally, you can use the contants in your database connection and any API code:

    $connect = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

Repo Sesources


About

A basic function to load variables from a .env file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages