View sourcecode

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

login.php

29 lines UTF-8 Unix (LF)
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
<?php
require_once __DIR__ '/functions.php';
require_once 
__DIR__ '/users.php';

if ((
$_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
    
redirect_with_flash('index.php''Fel metod för inloggning.');
}

if (empty(
$_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'] ?? ''$_POST['csrf_token'])) {
    
redirect_with_flash('index.php''Ogiltig förfrågan. Försök igen.');
}

$username trim($_POST['username'] ?? '');
$password = (string)($_POST['password'] ?? '');

$users load_users();

if (!isset(
$users[$username]) || !password_verify($password$users[$username][0])) {
    
redirect_with_flash('index.php''Användarnamn och/eller lösenord var fel.');
}

session_regenerate_id(true);
$_SESSION['user'] = [
    
'name'       => $username,
    
'role'       => $users[$username][1] ?? 'user',
    
'login_time' => time(),
];

redirect_with_flash('admin.php''Inloggning lyckades.');