Login ja logout

  1. Uus veebileht vorm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if(isset($_POST['login']) and isset($_POST['password'])){
    $login = $_POST['login'];
    $password = $_POST['password'];
    if ($login == 'admin' and $password == '1234'){
        $_SESSION['tuvastamine']='niilihtne';
        header('Location: ./puud.php');
    } else {
        echo "Invalid password.";
    }
}
<h1>Login vorm</h1>
<form action="" method="POST">
    Login:
    <input type="text" name="login" placeholder="username"><br>
    Parool:
    <input type="password" name="password"><br>
    <input type="submit" value="Logi sisse">
</form>

2. Veebilehel milles login

1
2
3
4
5
session_start();
if(!isset($_SESSION['tuvastamine'])) {
    header('Location: ./login.php');
    exit();
}

3. Logout

1
2
3
4
5
6
7
8
9
<?php
session_start();
if (isset($_POST['logout'])){
    session_unset();
    session_destroy();
    header('Location: login.php');
    exit();
}

CREATE TABLE kasutajad(
id int not null PRIMARY KEY AUTO_INCREMENT,
nimi varchar(10),
parool varchar(200),
onAdmin tinyint,
koduleht varchar(100))