-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.php
More file actions
132 lines (113 loc) · 3.65 KB
/
session.php
File metadata and controls
132 lines (113 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
// log.php - Página de inicio de sesión
// Configuración de la base de datos
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fichas2";
// Conexión a la base de datos
$conn = new mysqli($servername, $username, $password, $dbname);
// Verificar la conexión a la base de datos
if ($conn->connect_error) {
die("Error de conexión a la base de datos: " . $conn->connect_error);
}
// Establecer el conjunto de caracteres para la conexión
$conn->set_charset("utf8mb4");
session_start();
// Verificar si ya hay una sesión iniciada
if (isset($_SESSION['id_usuario'])) {
header("Location: fichas.php");
exit();
}
// Manejar el inicio de sesión si se recibe una solicitud POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
$username = $conn->real_escape_string($_POST['username']);
$password = $conn->real_escape_string($_POST['password']);
// Consulta para verificar las credenciales del usuario
$sqlVerificarUsuario = "SELECT id, username FROM usuarios WHERE username = '$username' AND password = '$password'";
$resultadoVerificarUsuario = $conn->query($sqlVerificarUsuario);
if ($resultadoVerificarUsuario === FALSE) {
echo "Error en la consulta SQL: " . $conn->error;
exit();
}
// Verificar si las credenciales son válidas
if ($resultadoVerificarUsuario->num_rows > 0) {
// Iniciar sesión y redirigir a la página principal
$usuario = $resultadoVerificarUsuario->fetch_assoc();
$_SESSION['id_usuario'] = $usuario['id'];
$_SESSION['nombre_usuario'] = $usuario['username'];
header("Location: fichas.php");
exit();
} else {
echo "Credenciales inválidas.";
}
}
// Cerrar la conexión a la base de datos
$conn->close();
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Iniciar Sesión</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Orbitron&display=swap">
<style>
body {
margin: 0;
padding: 0;
font-family: 'Orbitron', sans-serif;
background-color: #111;
color: #fff;
padding: 20px;
text-align: center;
}
.login-container {
width: 30%;
margin: auto;
margin-top: 100px;
}
form {
background-color: #222;
border: 1px solid #e44d26;
border-radius: 5px;
padding: 20px;
}
label {
display: block;
margin-bottom: 10px;
font-size: 18px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #e44d26;
background-color: #333;
color: #fff;
font-family: 'Orbitron', sans-serif;
border-radius: 3px;
}
button {
background-color: #e44d26;
color: #fff;
border: 1px solid #e44d26;
padding: 10px;
cursor: pointer;
font-size: 18px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="login-container">
<form action="" method="post">
<label for="username">Nombre de Usuario:</label>
<input type="text" name="username" required>
<label for="password">Contraseña:</label>
<input type="password" name="password" required>
<button type="submit" name="login">Iniciar Sesión</button>
</form>
</div>
</body>
</html>