48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
/**
|
|
* Load environment variables from a .env file.
|
|
* PHP 7.x compatible — no str_starts_with().
|
|
*/
|
|
if (!function_exists('loadEnv')) {
|
|
function loadEnv($path) {
|
|
if (!file_exists($path)) {
|
|
return;
|
|
}
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
if ($lines === false) return;
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || $line[0] === '#') {
|
|
continue;
|
|
}
|
|
$parts = explode('=', $line, 2);
|
|
if (count($parts) < 2) continue;
|
|
$key = trim($parts[0]);
|
|
$value = trim($parts[1]);
|
|
if (!array_key_exists($key, $_ENV)) {
|
|
$_ENV[$key] = $value;
|
|
putenv("$key=$value");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Try project root .env — works both locally and on InfinityFree
|
|
$envPath = __DIR__ . '/../.env';
|
|
loadEnv($envPath);
|
|
|
|
$host = getenv('DB_HOST') ?: ($_ENV['DB_HOST'] ?? '');
|
|
$ngalan = getenv('DB_USER') ?: ($_ENV['DB_USER'] ?? '');
|
|
$sikreto = getenv('DB_PASS') ?: ($_ENV['DB_PASS'] ?? '');
|
|
$deytabeys = getenv('DB_NAME') ?: ($_ENV['DB_NAME'] ?? '');
|
|
|
|
if (empty($host) || empty($ngalan)) {
|
|
die('<p style="color:red;font-family:monospace;padding:20px">DB config missing. Please create a .env file from .env.example</p>');
|
|
}
|
|
|
|
$mysqli = new mysqli($host, $ngalan, $sikreto, $deytabeys);
|
|
|
|
if ($mysqli->connect_error) {
|
|
die('<p style="color:red;font-family:monospace;padding:20px">Connection failed: ' . htmlspecialchars($mysqli->connect_error) . '</p>');
|
|
}
|
|
?>
|