IFCD0210/Practicas/Practicas_PHP/ejercicios/Plantilla Examen/registro.php
2024-03-04 13:49:12 +01:00

94 lines
2.9 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="estilos.css" />
<title>Registro de personal de hotel</title>
</head>
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$tipo_usuario = "";
if (!(isset($_GET["tipo"]) && ($_GET["tipo"] === 'cliente' || $_GET["tipo"] === 'hotel'))) {
header('Location: login.php');
}
$tipo_usuario = $_GET["tipo"];
}
?>
<body>
<div>
<h2>Registro
<?php
echo $tipo_usuario === 'hotel' ? "de personal del hotel" : "de clientes";
?>
</h2>
<form method="post">
<p style="display:flex; flex-direction: column; ">
<label for="nombre">Nombre:</label>
<input type="nombre" name="nombre" placeholder="Nombre" required>
</p>
<p style="display:flex; flex-direction: column; ">
<label for="email">Correo electrónico:</label>
<input type="email" name="email" placeholder="Email" required>
</p>
<p style="display:flex; flex-direction: column; ">
<label for="password">Contraseña:</label>
<input type="password" name="password" placeholder="Password" required>
</p>
<p>
<input type="submit" value="Registrar">
</p>
</form>
</div>
</body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validar si se han enviado los datos del formulario
if (isset($_POST["email"]) && isset($_POST["password"])) {
$nombre = trim($_POST["nombre"]);
$email = trim($_POST["email"]);
$password = password_hash(trim($_POST["password"]),PASSWORD_DEFAULT);
$tipo_usuario= trim($_GET["tipo"]);
// Validar si el archivo de almacenamiento existe, si no, crearlo
$archivo = "usuarios.txt";
if (!file_exists($archivo)) {
fopen($archivo, "w");
}
// Validar si el email ya está registrado
$usuarios = file_get_contents($archivo);
$lineas = explode("\n", $usuarios);
$email_existe = false;
foreach ($lineas as $linea) {
$datos = explode(":", $linea);
if (isset($datos[1]) && ($datos[1] == $email)) {
$email_existe = true;
break;
}
}
// Si el email no está registrado, almacenarlo en el archivo
if (!$email_existe) {
$nueva_linea = $nombre . ":" . $email . ":" . $password . ":" . $tipo_usuario . "\n";
file_put_contents($archivo, $nueva_linea, FILE_APPEND | LOCK_EX);
header('Location: login.php');
exit();
} else {
echo "Este email ya está registrado.";
}
} else {
echo "Por favor, complete todos los campos.";
}
}
?>
</html>