Last active
October 10, 2018 10:40
-
-
Save glenux/a727a02614b2d37a963967d1b6759073 to your computer and use it in GitHub Desktop.
Helper for PHP sites hosted on servers where environement variables are disabled in apache .htaccess (making mod_env and SetEnv useless)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// dotenv.php | |
// A helper script for PHP sites hosted on servers where environement variables | |
// are disabled in apache .htaccess (making mod_env and SetEnv useless) | |
// URL: https://gist.github.com/glenux/a727a02614b2d37a963967d1b6759073 | |
function dotenv_file() { | |
$dotenv = null; | |
foreach(['.', '..', '~/'] as $dir) { | |
$file = implode(DIRECTORY_SEPARATOR, [$dir, '.env']); | |
if (file_exists($file)) { | |
$dotenv = $file; | |
break; | |
} | |
} | |
return $dotenv; | |
} | |
function dotenv_load($file) { | |
foreach (file($file) as $line) { | |
if (preg_match('/^(\w+?)=(.+?)$/', $line)) { putenv($line); } | |
} | |
} | |
$envfile = dotenv_file(); | |
if ($envfile) { dotenv_load($envfile); } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment