-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connection.php
More file actions
31 lines (28 loc) · 909 Bytes
/
Copy pathdb_connection.php
File metadata and controls
31 lines (28 loc) · 909 Bytes
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
<?php
// db_connection.php
function getDBConnection()
{
// We need the session for accessing $_SESSION['dbUser'] / $_SESSION['dbPass']
// If the session is not started yet, start it:
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Make sure the user is logged in (i.e., has credentials in session):
if (!isset($_SESSION['dbUser'], $_SESSION['dbPass'])) {
// If not, redirect or throw an exception
header('Location: login.php');
exit;
}
try {
$pdo = new PDO(
'mysql:host=localhost;dbname=Labirinto;charset=utf8mb4',
$_SESSION['dbUser'],
$_SESSION['dbPass'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
return $pdo;
} catch (PDOException $e) {
// Handle or rethrow
die("Database connection error: " . $e->getMessage());
}
}