View sourcecode

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

login.php

34 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
30
31
32
33
34
<?php
session_start
();

// Associativ array: användare => lösenord
$users json_decode(file_get_contents('users.json'),true);

// Kontrollera om formuläret har skickats
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
$username $_POST['username'] ?? '';
    
$password $_POST['password'] ?? '';
    
$KeepLoggedIn = isset($_POST['keepLoggedIn']);

    
// Kolla om användaren finns och lösenordet stämmer
    
if (isset($users[$username]) && password_verify($password$users[$username])) {
        
$_SESSION['logged_in'] = true;
        
$_SESSION['username'] = $username;

        
// Håll mig inloggad med cookie
        
if ($KeepLoggedIn) {
            
setcookie('logged_in''true'time() + 3600'/');
            
setcookie('username'$usernametime() + 3600'/');
        }

        
// Skicka vidare till admin.php
        
header('Location: admin.php');
        exit;
    } else {
        
// Fel inloggning
        
header('Location: index.php?error=1');
        exit;
    }
}