-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
66 lines (55 loc) · 1.65 KB
/
db.php
File metadata and controls
66 lines (55 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
// db.php
/* Charge .env */
function loadEnv($path)
{
if (!file_exists($path)) {
throw new Exception("Le fichier .env n'existe pas.");
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
// Ignore les commentaires
if (strpos(trim($line), '#') === 0) {
continue;
}
// Sépare la clé et la valeur
if (strpos($line, '=') !== false) {
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value);
// Enleve les guillemets
$value = str_replace(['"', "'"], '', $value);
// Stocke dans les variables d'environnement PHP
$_ENV[$name] = $value;
}
}
}
// Charge les variables .env
try {
loadEnv(__DIR__ . '/.env');
} catch (Exception $e) {
die("Erreur de configuration : " . $e->getMessage());
}
// Récupère les infos depuis le .env
$host = $_ENV['DB_HOST'];
$db = $_ENV['DB_NAME'];
$user = $_ENV['DB_USER'];
$pass = $_ENV['DB_PASS'];
$charset = 'utf8mb4';
// Connexion PDO
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
// --- TRACKING DES JOUEURS EN LIGNE ---
if (isset($_SESSION['user_id'])) {
$pdo->prepare("UPDATE users SET last_activity = NOW() WHERE id = ?")->execute([$_SESSION['user_id']]);
}
} catch (\PDOException $e) {
die("Erreur.");
}
?>