Diff between 71217f1188921b15dd652568d8516abf13261b26 and a8beb03f69571ec952c5ba59a0cf4a6e6946a496

Changed Files

File Additions Deletions Status
admin.php +25 -0 added
database.php +18 -0 added
index.php +6 -2 modified
login.php +65 -0 added
logout.php +8 -0 added

Full Patch

diff --git a/admin.php b/admin.php
new file mode 100644
index 0000000..de68c36
--- /dev/null
+++ b/admin.php
@@ -0,0 +1,25 @@
+<?php 
+require_once "database.php";
+
+// estä pääsy muilta paitsi opettajilta
+if (!isset($_SESSION['user_id'])) {
+    echo "Sinulla ei ole riittäviä oikeuksia";
+    die();
+}
+
+?>
+
+<!DOCTYPE html>
+<html lang="fi">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Opettaja näkymä</title>
+</head>
+<body>
+    <?php echo $_SESSION['user_name']; ?>
+    <a href="logout.php">
+        Kirjaudu ulos
+    </a>
+</body>
+</html>
\ No newline at end of file
diff --git a/database.php b/database.php
new file mode 100644
index 0000000..17f5e65
--- /dev/null
+++ b/database.php
@@ -0,0 +1,18 @@
+<?php
+// aloittaa session ja luo yhteyden tietokantaan
+if (session_status() === PHP_SESSION_NONE) {
+    session_start();
+}
+
+$servername = "localhost";
+$username = "root";
+$password = "";
+$database = "taitaja2025";
+
+$conn = new mysqli($servername, $username, $password, $database);
+$conn->set_charset("utf8mb4");
+
+if ($conn->connect_error) {
+    die("failed to connect: " . $conn->connect_error);
+}
+?> 
\ No newline at end of file
diff --git a/index.php b/index.php
index 353e2b1..e8e957b 100644
--- a/index.php
+++ b/index.php
@@ -1,3 +1,7 @@
 <?php
-    echo "Taitaja 2025"
-?>
\ No newline at end of file
+require_once "database.php"
+?>
+
+<a href="login.php">
+    Kirjaudu sisään
+</a>
\ No newline at end of file
diff --git a/login.php b/login.php
new file mode 100644
index 0000000..57ea947
--- /dev/null
+++ b/login.php
@@ -0,0 +1,65 @@
+<?php
+require_once '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 id, name, password FROM users WHERE name = '{$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'])) {
+                $_SESSION['user_id'] = $user['id'];
+                $_SESSION['user_name'] = $user['name'];
+                header('Location: 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>
diff --git a/logout.php b/logout.php
new file mode 100644
index 0000000..be5d201
--- /dev/null
+++ b/logout.php
@@ -0,0 +1,8 @@
+<?php
+// tuhoaa session, mikä "kirjauduttaa" käyttäjän ulos
+require_once 'database.php';
+
+session_destroy();
+header('Location: index.php');
+exit();
+?>
\ No newline at end of file