Visa Källkod

The following files exist in this folder. Click to view.

NamnTypStorlek
account.phpPHP Fil11.6 KB
admin.phpPHP Fil23.6 KB
authors.phpPHP Fil4.1 KB
book.phpPHP Fil5.9 KB
db_cnnt.phpPHP Fil407 B
header.phpPHP Fil1.7 KB
home.phpPHP Fil8.4 KB
index.phpPHP Fil5.3 KB
loan_handler.phpPHP Fil1.9 KB
manage_authors.phpPHP Fil4.7 KB
my_loans.phpPHP Fil6.9 KB
S.sqlSQL Fil3.3 KB
search.phpPHP Fil11 KB

header.php

45 lines UTF-8 Unix (LF) - Type: PHP Fil
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
<?php

// Starta session om den är inte redan startad
if (session_status() === PHP_SESSION_NONE) {
    
// Session-ssäkerhets-inställningar -
    
ini_set('session.cookie_httponly'1);  // Förhindra JS-åtkomst till cookies (alltså XSS-skydd)
    
ini_set('session.use_only_cookies'1); // Skicka sessions-ID endast via cookies (inte URL) (skydd mot session fixation)
    
ini_set('session.cookie_secure'0);    // Skicka cookies endast över HTTPS
    
    
session_start();
}

// Hämta användarens roll om inloggad
$userRole $_SESSION['role'] ?? '';
if (isset(
$_SESSION['user_id']) && empty($userRole)) {
    require_once 
'db_cnnt.php';
    global 
$pdo;
    
$statement $pdo->prepare("SELECT r.roll_namn FROM användare a JOIN roll r ON a.roll_id = r.roll_id WHERE a.användare_id = ?");
    
$statement->execute([$_SESSION['user_id']]);
    
$row $statement->fetch();
    if (
$row) {
        
$userRole $row['roll_namn'];
        
$_SESSION['role'] = $userRole// Cacha i session  
    
}
}
?>
<header>
    <nav class="navbar">
        <ul>
            <?php if (isset($_SESSION['user_id'])): ?>
                <li><a href="home.php">Hem</a></li>
                <li><a href="search.php">Sök</a></li>
                <li><a href="my_loans.php">Mina lån</a></li>
                <?php if ($userRole === 'Admin'): ?>
                    <li><a href="admin.php">Admin</a></li>
                <?php endif; ?>
                <li><a href="account.php">Mitt konto</a></li>
                <li><a href="account.php?action=logout">Logga ut</a></li>
            <?php else: ?>
                <li><a href="index.php">Logga in</a></li>
            <?php endif; ?>
        </ul>
    </nav>
</header>