Update: 2026-05-14 19:54:11

This commit is contained in:
Hamza-Ayed
2026-05-14 19:54:11 +03:00
parent e1654be362
commit 90fe9eb1c3

View File

@@ -1,13 +1,43 @@
<?php
// backend/config/db.php
header('Content-Type: application/json');
require_once __DIR__ . '/../vendor/autoload.php';
use Dotenv\Dotenv;
// Load environment variables from .env (located one level above the project root)
$dotenv = Dotenv::createImmutable(__DIR__ . '/../../../..');
$dotenv->load();
// Try to load .env from multiple possible locations
$envLoaded = false;
$searchPaths = [
__DIR__ . '/../..', // jordan_bot/
__DIR__ . '/../../..', // lawer.tripz-egypt.com/
__DIR__ . '/../../../..', // htdocs/
__DIR__ . '/../../../../..', // home directory
posix_getpwuid(posix_getuid())['dir'] ?? '', // PHP-detected home dir
];
foreach ($searchPaths as $path) {
if (!empty($path) && file_exists($path . '/.env')) {
try {
$dotenv = Dotenv::createImmutable($path);
$dotenv->load();
$envLoaded = true;
break;
} catch (Exception $e) {
// Try next path
}
}
}
if (!$envLoaded) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '.env file not found. Searched paths: ' . implode(', ', array_filter($searchPaths))
]);
exit;
}
$host = $_ENV['DB_HOST'] ?? 'localhost';
$dbname = $_ENV['DB_NAME'] ?? 'jordan_bot_db';
@@ -19,7 +49,6 @@ try {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// In production, you should log this error and show a generic message
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Database connection failed: ' . $e->getMessage()]);
exit;