Download all files: Download
Task:
- First, create some useful functions and select them in a separate file:
- The getUsersList () function let returns an array of all users and their password hashes
- The existsUser ($ login) function checks if a user exists with the specified login?
- The function sheckPassword ($ login, $ password) is allowed to return true when there is a user with the specified login and the password entered by him has passed the test
- Add a getCurrentUser () function that returns either the name of the user logged into the site, or null
- Add a login.php page to the project, which:
- IF the user has already logged in (see point 2), THEN redirect to the main page
- IF user is not logged in - displays the login form
- IF the data is entered into the login form - we check it (see clause 1.3) and IF the check is passed, THEN we remember the information about the logged in user
Problem: I want to exit the user from the site, but I can’t delete cookies, a piece of code from login.php:
<?php if (0 !== count($_COOKIE)) { ?> <script type="text/javascript"> let linkExit = document.getElementById('exit'); linkExit.addEventListener('onclick', function () { <?php setcookie(key($_COOKIE), $_COOKIE[key($_COOKIE)], time() - 100); $_COOKIE = []; ?> }) </script> <?php } echo 'Вы уже вошли!' . '<br/>';?> Because I inserted a piece of js (to track the exit button pressed), this does not allow cookies to be deleted
index.php:
<?php include __DIR__ . '/functions.php'; if (checkPassword($_POST['login'], $_POST['password'])) { setcookie($_POST['login'], getUsersList()[$_POST['login']]); echo 'Вы вошли как ' . key($_COOKIE); } else { echo 'Вы не вошли или ввели неверные данные'; ?> <br/> <a href="login.php">Войти</a> <?php } ?> <!doctype html> <html lang="ru"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> </html> login.php:
<?php if (0 !== count($_COOKIE)) { ?> <script type="text/javascript"> let linkExit = document.getElementById('exit'); linkExit.addEventListener('onclick', function () { <?php setcookie(key($_COOKIE), $_COOKIE[key($_COOKIE)], time() - 100); $_COOKIE = []; ?> }) </script> <?php echo 'Вы уже вошли!' . '<br/>';?> <a href="login.php" id="exit">Выйти</a> <!doctype html> <html lang="ru"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Логин</title> </head> <body> <?php } else {?> <form action="index.php" method="post"> <p>Войдите на сайт!</p> <input type="text" name="login"> <input type="password" name="password"> <button type="submit">Войти!</button> </form> <?php } ?> </body> </html> functions.php:
<?php function getUsersList () { $arr = [ 'admin' => '12345', 'lepta' => '54321', ]; foreach ($arr as $key => $elem) { $arr[$key] = password_hash($elem, PASSWORD_DEFAULT); } return $arr; } function existsUser($login) { $arr = getUsersList(); $result = false; foreach ($arr as $key => $elem) { if ($key === $login) { $result = true; break; } } return $result; } function checkPassword($login, $password) { $arr = getUsersList(); $result = false; foreach ($arr as $key => $elem) { if ($key === $login && password_verify($password, $elem)) { $result = true; break; } } return $result; } ?>