1. Go to this page and download the library: Download devuri/env library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
devuri / env example snippets
use Urisoft\Env;
// Initialize the Env with a whitelist of environment variables
$env = new Env([
'APP_KEY', 'DB_HOST', 'DB_NAME', // Add your environment variables here
]);
// Retrieve an environment variable
$dbHost = $env->get('DB_HOST');
// Retrieve an environment variable with a default value
$debugMode = $env->get('DEBUG_MODE', false);
$env = new Env([], '/path/to/encryption/key');
// Retrieve and encrypt an environment variable
$encryptedAppKey = $env->get('APP_KEY', 'secret value', true );
$env->setWhitelist([
'NEW_VAR_1', 'NEW_VAR_2', // Add new variables as needed
]);
use Urisoft\Env;
/**
* Retrieves a sanitized, and optionally encrypted or modified, environment variable by name.
*
* @param string $name The name of the environment variable to retrieve.
* @param mixed $default Default value to return if the environment variable is not set
* @param bool $encrypt Indicate that the value should be encrypted. Defaults to false.
* @param bool $strtolower Whether to convert the retrieved value to lowercase. Defaults to false.
*
* @throws InvalidArgumentException If the requested environment variable name is not in the whitelist
* or if encryption is requested but the encryption path is not defined.
*
* @return mixed The sanitized environment variable value, possibly encrypted or typecast,
* or transformed to lowercase if specified.
*/
function env( $name, $default = null, $encrypt = false, $strtolower = false ) {
// Define your whitelist and encryption path here. These could also be fetched from a configuration file or another source.
$whitelist = [
// Add your environment variables to the whitelist
];
$encryptionPath = '/path/to/your/encryption/key';
// Create an instance of the Env class with your predefined settings
static $env = null;
if ($env === null) {
$env = new Env($whitelist, $encryptionPath);
}
// Use the Env instance to get the environment variable value
return $env->get($name, $default, $encrypt, $strtolower);
}