EnvSecured is a lightweight, secure, and self-contained PHP module for storing sensitive configuration values (API keys, database credentials, tokens, secrets) in an encrypted file and provides a clean interface to access them in runtime.
- π Encrypted config file (
config.enc) - π Browser-based UI for editing settings
- π€ JSON export (download)
- π₯ JSON import (load file into form)
- π Automatic key generation (
keys/*.key) - 𧬠Server-bound encryption (fingerprint-based)
- π§© Zero global functions β everything wrapped in PHP classes
- π Drop-in integration into any project
- βοΈ Can be used:
- with Composer
- without Composer
env_secured/
βββ _init.php β Bootloader (entry point)
βββ libs/
β βββ EnvSecured.php β Main config manager
β βββ EnvSecuredCrypto.php β Encryption engine
β βββ html/
β βββ page_form.php β UI template: config editor
β βββ page_success.php β UI template: success page
β βββ page_error.php β UI template: error page
βββ configs/ β Encrypted config files (auto-created)
β βββ config.enc β Main encrypted config (auto-created)
βββ keys/ β Key files (auto-created)
βββ sodium.key β Internal crypto key
βββ secret.key β Master secret key
Both configs/ and keys/ directories are created automatically on first use if they do not exist.
composer require hegelmax/env-securedDownload the directory:
env_secured/
and place it anywhere in your project.
require __DIR__ . '/vendor/autoload.php';
use EnvSecured\EnvSecured;
$envRoot = __DIR__ . '/env'; // Directory for configs/ and keys/
$env = new EnvSecured($envRoot);
$env->run();
// Retrieve configuration
$config = EnvSecured::get(); // full array
$dbHost = EnvSecured::get('DB_HOST'); // single valuerequire __DIR__ . '/env_secured/init.php';Then read configuration via:
$env = EnvSecured::get(); // array
echo EnvSecured::get('API_URL'); When no encrypted config exists, opening your init script in a browser shows the Config Editor UI:
/env_secured/init.php
UI allows:
Folders created automatically:
env/
configs/
config.enc
keys/
sodium.key
secret.key
EnvSecured uses:
- 256-bit
sodium.key - 256-bit
secret.key - machine + project fingerprint
- XSalsa20-Poly1305 (libsodium)
- unique nonce per encryption
- atomic writes to prevent corruption
Conceptually:
fingerprint = HASH( hostname | projectRoot | secret.key )
finalKey = HASH( fingerprint | sodium.key )
cipher = base64( nonce | secretbox(plaintext, nonce, finalKey) )
- Keys stored outside web root (in
env_secured/keys/) - Config stored encrypted (
env_secured/configs/config.enc) - No plaintext config on server
- No global functions β no name collisions
- Atomic writes for safe file operations
- Encryption relies on libsodium (modern & secure)
Once EnvSecured loads the config:
$config = EnvSecured::get();
echo $config['DB_HOST'];echo EnvSecured::get('API_TOKEN');If constant autodefine is enabled:
echo API_TOKEN;Enable via:
const ENV_SECURED_CONFIG_DEFINE_CONST = true;Place them before calling EnvSecured.
const ENV_SECURED_CONFIG_SCHEMA = 'prod';
const ENV_SECURED_CONFIG_ALLOW_EDIT = false;
const ENV_SECURED_CONFIG_ALLOW_SESSION = true;
const ENV_SECURED_CONFIG_DEFINE_CONST = true;
const ENV_SECURED_DEFAULTS = [
['key' => 'DB_HOST', 'value' => 'localhost'],
['key' => 'API_URL', 'value' => 'https://localhost/api'],
];- PHP 8.1+
ext-sodiumenabled- Writable directory for:
configs/keys/
EnvSecured supports configuration migration via JSON file, that can be useful for:
- migrations
- backups
- moving configs between servers
- Dev β Prod workflows
Downloads a readable .json file containing all config values.
Loads a .json file directly in the browser and fills the config form.
No data is sent to the server until Save (encrypted) is pressed.
- On old server β open UI β Download JSON
- Transfer the downloaded file to the new server
- On new server β open UI β Load JSON
- Click Save (encrypted)
A new encrypted config is generated automatically for the new environment; secret keys remain private.
Temporary snippet:
require_once __DIR__ . '/env_secured/_init.php';
$cipher = (new EnvSecuredCrypto(__DIR__ . '/env_secured'))->encrypt("test");
var_dump($cipher);Then ensure:
(new EnvSecuredCrypto(__DIR__ . '/env_secured'))->decrypt($cipher) === "test";MIT License. Free for commercial use.
Β© 2025 Maxim Hegel