security update

This commit is contained in:
2026-01-19 08:57:58 +03:00
parent 55b3d5f088
commit 5c084c6aaa
8 changed files with 182 additions and 49 deletions

View File

@@ -1,37 +1,47 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
header('Content-Type: application/json; charset=utf-8');
require_once '../config/db.php';
require_once '../config/session.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
$login = trim($data['username'] ?? '');
$password = $data['password'] ?? '';
// Поиск пользователя по username
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$login]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password_hash'])) {
// Успешная авторизация
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
echo json_encode([
'success' => true,
'message' => 'Вход выполнен успешно',
'user' => [
'id' => $user['id'],
'username' => $user['username'],
]
]);
} else {
echo json_encode(['success' => false, 'message' => 'Неверные учетные данные']);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'message' => 'Метод не поддерживается']);
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
if (!is_array($data)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Некорректные данные']);
exit;
}
$login = trim($data['username'] ?? '');
$password = $data['password'] ?? '';
// Поиск пользователя по username
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$login]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password_hash'])) {
// Успешная авторизация
session_regenerate_id(true);
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
echo json_encode([
'success' => true,
'message' => 'Вход выполнен успешно',
'user' => [
'id' => $user['id'],
'username' => $user['username'],
]
]);
} else {
echo json_encode(['success' => false, 'message' => 'Неверные учетные данные']);
}
?>