Skip to content

Instantly share code, notes, and snippets.

@glenux
Last active October 10, 2018 10:40
Show Gist options
  • Save glenux/a727a02614b2d37a963967d1b6759073 to your computer and use it in GitHub Desktop.
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)
<?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