clean debug

This commit is contained in:
Lain Iwakura 2025-06-16 02:30:02 +03:00
parent 4f35b9f663
commit 545df04525
No known key found for this signature in database
GPG Key ID: C7C18257F2ADC6F8

View File

@ -9,10 +9,8 @@ header('Content-Security-Policy: default-src \'self\'; style-src \'self\' \'unsa
header('Strict-Transport-Security: max-age=31536000; includeSubDomains'); header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
session_start(); session_start();
$debug = [];
try { try {
$config = require 'config.php'; $config = require 'config.php';
$debug[] = "Config loaded";
$db = new PDO( $db = new PDO(
"mysql:host={$config['db']['host']};dbname={$config['db']['name']}", "mysql:host={$config['db']['host']};dbname={$config['db']['name']}",
@ -20,9 +18,7 @@ try {
$config['db']['pass'] $config['db']['pass']
); );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$debug[] = "Database connected";
} catch (PDOException $e) { } catch (PDOException $e) {
$debug[] = "Database error: " . $e->getMessage();
die("Database connection error: " . $e->getMessage()); die("Database connection error: " . $e->getMessage());
} }
@ -32,46 +28,35 @@ $success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING); $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$debug[] = "Login attempt for: " . $username;
if ($username && $password) { if ($username && $password) {
try { try {
$stmt = $db->prepare('SELECT id, password, is_blocked, login_attempts, last_attempt FROM users WHERE username = ?'); $stmt = $db->prepare('SELECT id, password, is_blocked, login_attempts, last_attempt FROM users WHERE username = ?');
$stmt->execute([$username]); $stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC); $user = $stmt->fetch(PDO::FETCH_ASSOC);
$debug[] = "User found: " . ($user ? 'yes' : 'no');
if ($user) { if ($user) {
if ($user['is_blocked']) { if ($user['is_blocked']) {
$error = 'Account is blocked'; $error = 'Account is blocked';
$debug[] = "Account blocked";
} else if ($user['login_attempts'] >= 5 && strtotime($user['last_attempt']) > strtotime('-15 minutes')) { } else if ($user['login_attempts'] >= 5 && strtotime($user['last_attempt']) > strtotime('-15 minutes')) {
$error = 'Too many login attempts'; $error = 'Too many login attempts';
$debug[] = "Too many attempts";
} else if (password_verify($password, $user['password'])) { } else if (password_verify($password, $user['password'])) {
$stmt = $db->prepare('UPDATE users SET login_attempts = 0, last_attempt = NOW() WHERE id = ?'); $stmt = $db->prepare('UPDATE users SET login_attempts = 0, last_attempt = NOW() WHERE id = ?');
$stmt->execute([$user['id']]); $stmt->execute([$user['id']]);
$_SESSION['user_id'] = $user['id']; $_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $username; $_SESSION['username'] = $username;
$debug[] = "Login successful";
header('Location: index.php'); header('Location: index.php');
exit; exit;
} else { } else {
$stmt = $db->prepare('UPDATE users SET login_attempts = login_attempts + 1, last_attempt = NOW() WHERE id = ?'); $stmt = $db->prepare('UPDATE users SET login_attempts = login_attempts + 1, last_attempt = NOW() WHERE id = ?');
$stmt->execute([$user['id']]); $stmt->execute([$user['id']]);
$error = 'Invalid password'; $error = 'Invalid password';
$debug[] = "Invalid password";
} }
} else { } else {
$error = 'User not found'; $error = 'User not found';
$debug[] = "User not found";
} }
} catch (PDOException $e) { } catch (PDOException $e) {
$error = 'Server error'; $error = 'Server error';
$debug[] = "SQL Error: " . $e->getMessage();
$debug[] = "SQL State: " . $e->getCode();
} }
} }
} }
@ -88,7 +73,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
button { width: 100%; padding: 10px; background: #2196F3; color: white; border: none; cursor: pointer; } button { width: 100%; padding: 10px; background: #2196F3; color: white; border: none; cursor: pointer; }
.error { color: red; } .error { color: red; }
.success { color: green; } .success { color: green; }
.debug { background: #f5f5f5; padding: 10px; margin: 10px 0; font-family: monospace; }
</style> </style>
</head> </head>
<body> <body>
@ -110,15 +94,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<button type="submit">Login</button> <button type="submit">Login</button>
</form> </form>
<p><a href="register.php">Register</a> | <a href="index.php">Back to chat</a></p> <p><a href="register.php">Register</a> | <a href="index.php">Back to chat</a></p>
<?php if (!empty($debug)): ?>
<div class="debug">
<strong>Debug info:</strong><br>
<?php foreach ($debug as $line): ?>
<?= htmlspecialchars($line) ?><br>
<?php endforeach; ?>
</div>
<?php endif; ?>
</body> </body>
</html> </html>
<?php ob_end_flush(); ?> <?php ob_end_flush(); ?>