Blob: login.php

Blob id: 967a9b6c8c1a984e4b5d1d757039bd099af4c800

Size: 1.9 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
<?php
require_once '../src/database.php';

// kirjautuminen
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // ota tiedot formista
    $name = mysqli_real_escape_string($conn, trim($_POST['name'] ?? ''));
    $password = mysqli_real_escape_string( $conn, trim($_POST['password'] ?? ''));
    
    if (!empty($name) && !empty($password)) {
        // etsi käyttäjä
        $sql = "SELECT * FROM teachers WHERE username = '{$name}'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            $user = $result->fetch_assoc();

            // tarkista salasanan hash, php defaultti (bcrypt)
            if (password_verify($password, $user['password_hash'])) {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['name'];
                header('Location: ../public/admin.php');
                exit();
            }
        }

        // katkaise yhteys tietokantaan
        $conn->close();
    }
}
?>

<html lang="fi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kirjaudu</title>
</head>
<body>
    <h1>Kirjaudu opettajana</h1>
    <form method="POST" action="">
        <fieldset>
            <table>
                <tr>
                    <td>
                        <label for="name">Nimi:</label>
                    </td>
                    <td>
                        <input type="text" name="name" required>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="password">Salasana:</label>
                    </td>
                    <td>
                        <input type="password" name="password" required>
                    </td>
                </tr>
            </table>
        </fieldset>
        <button type="submit">Kirjaudu</button>
    </form>
</body>
</html>