Visa Källkod

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

NamnTypStorlek
admin.phpPHP Fil7.4 KB
change_password.phpPHP Fil1.6 KB
check_login.phpPHP Fil183 B
delete_user.phpPHP Fil641 B
guestbook.jsonJSON Fil623 B
guestbook.phpPHP Fil5.5 KB
index.phpPHP Fil1.9 KB
login.phpPHP Fil1.6 KB
logout.phpPHP Fil341 B
register.phpPHP Fil872 B
users.jsonJSON Fil1.9 KB

login.php

62 lines ISO-8859-1 BOM 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
session_start
();

$filename "users.json";
$users = [];
if (
file_exists($filename)) {
    
$users json_decode(file_get_contents($filename), true) ?? [];
}

$found false;
$role "user";

// Kollar om formuläret har skickats in
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
$username $_POST['username'] ?? '';
    
$password $_POST['password'] ?? '';

    foreach (
$users as $user) {
        if (
$user['username'] === $username && password_verify($password$user['password'])) {
            
$found true;
            
$role $user['role'] ?? "user";
            break;
        }
    }
    
    if (
$found) {
        
$_SESSION['loggedin'] = true;
        
$_SESSION['username'] = $username;
        
$_SESSION['role'] = $role;
        
header("Location: admin.php");
        exit;
    } else {
        
header("Location: index.php?error=1");
        exit;
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Inloggningsapplikation</title>
    <link rel="stylesheet" href="style.css">
    <link rel="icon" href="../../m02/Favicon-a.jpg" type="image/x-icon">
</head>
<body>
<form method="POST" action="">
    <label for="username">Användarnamn:</label>
    <input type="text" name="username" id="username" required>
    <br>
    <label for="password">Lösenord:</label>
    <input type="password" name="password" id="password" required>
    <br>
    <input type="submit" value="Logga in">
</form>
</body>
</html>