add session

This commit is contained in:
Aleksandr Shelkovin 2025-01-20 22:31:07 +03:00
parent 70446d9b3a
commit 833a47ae43
3 changed files with 65 additions and 6 deletions

View File

@ -1,8 +1,22 @@
<?php <?php
require_once __DIR__ . "/../view/View.php"; session_start();
class AdminController {
public function actionIndex() {
echo (View::render([], 'admin.php'));
require_once __DIR__ . "/../view/View.php";
class AdminController
{
public function actionIndex()
{
if (
isset($_SESSION['IS_AUTH'])
and $_SESSION['IS_AUTH'] == true
) {
echo (View::render([], 'admin.php'));
} else {
header("Location:http://localhost:8000/login");
};
} }
} }

View File

@ -0,0 +1,45 @@
<?php
session_start();
class AuthController
{
const PASSWD = 123;
const LOGIN = 'none@none.ru';
const DOMAIN = 'http://localhost:8000/admin';
public $login = null;
public $password = null;
public function __construct()
{
$this->login = $_POST['login'];
$this->password = $_POST['password'];
}
public function actionLogin()
{
if (($this->login == self::LOGIN) && ($this->password == self::PASSWD)) {
$_SESSION['IS_AUTH'] = true;
// редирект в админку
// setcookie("login", $this->login, time() + 3600); // Истекает через 1 час
// setcookie("password", $this->password, time() + 3600); // Истекает через 1 час
// header("Set-Cookie: login=$login; Secure; Path=/; SameSite=None; Partitioned;");
// header("Set-Cookie: passwd=$password; Secure; Path=/; SameSite=None; Partitioned;");
header("Location:". self::DOMAIN);
} else {
// редирект на форму логина с предупреждением
header("Location:http://localhost:8000/login");
}
// var_dump($this->login);
// var_dump($this->password);
// echo ("LOGIN HANDLER");
}
public function actionLogout()
{
echo ('print Logout');
}
}

View File

@ -6,7 +6,7 @@ return array(
'about' => 'about/index', 'about' => 'about/index',
'admin' => 'admin/index', 'admin' => 'admin/index',
'login' => 'login/index', 'login' => 'login/index',
'auth' => 'auth/index', 'auth' => 'auth/login',
); );
// - где 'news' - строка запроса // - где 'news' - строка запроса
// 'news/index' - имя контроллера и экшена для обработки этого запроса (путь обработчика) // 'news/index' - имя контроллера и экшена для обработки этого запроса (путь обработчика)