110 lines
4.4 KiB
PHP
110 lines
4.4 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header('Location: pedidos.php');
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'];
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = 'Please enter both username and password.';
|
|
} else {
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->prepare('SELECT * FROM users WHERE username = :username');
|
|
$stmt->bindParam(':username', $username);
|
|
$stmt->execute();
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['user_role'] = $user['role'];
|
|
|
|
// Record session
|
|
try {
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
|
$ua = $_SERVER['HTTP_USER_AGENT'] ?? 'unknown';
|
|
$ciudad = 'Desconocida';
|
|
|
|
if ($ip !== 'unknown' && $ip !== '127.0.0.1' && $ip !== '::1') {
|
|
$geo = @file_get_contents("http://ip-api.com/json/{$ip}?fields=city");
|
|
if ($geo) {
|
|
$geoData = json_decode($geo, true);
|
|
if (isset($geoData['city'])) {
|
|
$ciudad = $geoData['city'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$stmtSession = $db->prepare('INSERT INTO user_sessions (user_id, ip_address, ciudad, user_agent) VALUES (:user_id, :ip, :ciudad, :ua)');
|
|
$stmtSession->bindParam(':user_id', $user['id']);
|
|
$stmtSession->bindParam(':ip', $ip);
|
|
$stmtSession->bindParam(':ciudad', $ciudad);
|
|
$stmtSession->bindParam(':ua', $ua);
|
|
$stmtSession->execute();
|
|
} catch (PDOException $e) {
|
|
// Silently fail if session recording fails, don't block login
|
|
}
|
|
|
|
header('Location: pedidos.php');
|
|
exit();
|
|
} else {
|
|
$error = 'Invalid username or password.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login - FLOWERSEGUIMIENTOPEDIDOS</title>
|
|
<meta property="og:title" content="FLOWERSEGUIMIENTOPEDIDOS">
|
|
<meta property="og:image" content="">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="twitter:image" content="">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/style.css">
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6 col-lg-4">
|
|
<div class="card mt-5">
|
|
<div class="card-body">
|
|
<h3 class="card-title text-center">Login</h3>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Login</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|