Login
$usuarios=[
'miguel'=>["blucod123", "Admin"],
'pepito'=>["pepito123", "Normal"],
'juanito'=>["juanito123", "Gestor"]
];
<?php
session_start();
require "usuarios.php";
if(isset($_POST['btn'])){
$error=false;
$nombre=trim($_POST['nombre']);
$pass=$_POST['pass'];
if($error==true){
header("Location:{$_SERVER['PHP_SELF']}");
}
foreach($usuarios as $k=>$v){
if($k==$nombre && $pass==$v[0]){
$_SESSION['username']=$nombre;
$_SESSION['perfil']=$v[1];
header("Location:menu.php");
die();
}
}
//Validación incorrecta
$_SESSION['errValidacion']= "Nombre de usuario o contraseña incorrecta";
header("Location:{$_SERVER['PHP_SELF']}");
die();
}else{
//pintamos la página
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<form name="a" method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cerramos al final del documento
<?php } ?>
<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location_login.php");
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>menu</title>
</head>
<body style="background-color:aqua">
</br>
</br>
</br>
</br>
</br>
</br>
<div class="container mt-4" align="center">
<?php
if($_SESSION['perfil']=="Admin"){
echo "<a href='main.php' class='btn btn-primary'>Ir a la Página 1</a>";
}
?>
</div>
<div class="container mt-4" align="center">
<a href='login.php' class="btn btn-danger">Cerrar Sesion</a>
</div>
</body>
</html>
<?php
session_start();
session_destroy();
header("Location:login.php");
mysql
public
session_start();
require_once __DIR__ . "/../db/conexion.php";
$q = "select * from tabla";
$resultado = mysqli_query($llave, $q);
mysqli_close($llave);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>usuarios</title>
</head>
<body>
<table class="table table-dark table-striped mt-5">
<thead>
<tr>
<th scope="col">dni</th>
<th scope="col">nombre</th>
<th scope="col">apellido</th>
</tr>
</thead>
<tbody>
<?php
while ($fila = mysqli_fetch_assoc($resultado)) {
echo <<<TXT
<tr>
<td>{$fila['dni']}</td>
<td>{$fila['nombre']}</td>
<td>{$fila['apellido']}</td>
</tr>
TXT;
}
?>
</tbody>
</table>
<a href="nuevo.php" class="btn btn-primary">Agregar usuario</a>
<br>
<br>
<a href="borrar.php" class="btn btn-danger">Borrar usuario</a>
<br>
<br>
<a href="actualizar.php" class="btn btn-secondary">Actualizar usuario</a>
</body>
</html>
session_start();
if(isset($_POST['enviar'])){
$error=false;
//procesamos el form
require_once __DIR__."/../db/conexion.php";
$dni=trim($_POST['dni']);
$nombre=trim($_POST['nombre']);
$apellido=trim($_POST['apellido']);
//guardamos el coche
$q="insert into tabla(dni, nombre, apellido) values(?, ?, ?)";
$stmt=mysqli_stmt_init($llave);
if(mysqli_stmt_prepare($stmt, $q)){
mysqli_stmt_bind_param($stmt, 'sss', $dni, $nombre, $apellido);
mysqli_stmt_execute($stmt);
}else{
die("Error al insertar");
}
mysqli_stmt_close($stmt);
mysqli_close($llave);
$_SESSION['mensaje']="User agregado con éxito";
header("Location:main.php");
die();
}
else{
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="wdnith=device-wdnith, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5dnixbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>Agregar usuario</title>
</head>
<body style="background-color:black">
<h5 class="text-center mt-2">Agregar usuario</h5>
<div class="container">
<form name="aw" action="nuevo.php" method="POST" class="text-light">
<div class="mb-3">
<label for="n" class="form-label">dni</label>
<input type="text" class="form-control" id="dni" placeholder="dni" name="dni">
</div>
<div class="mb-3">
<label for="e" class="form-label">nombre</label>
<input type="text" class="form-control" id="nombre" placeholder="nombre" name="nombre">
</div>
<div class="mb-3">
<label for="e" class="form-label">apellido</label>
<input type="text" class="form-control" id="apellido" placeholder="apellido" name="apellido">
</div>
</div>
<div class="d-flex">
<button type="submit" class="btn btn-primary btn-lg btn-block" name="enviar">Enviar</button>
</button>
<button type="delete" class="btn btn-secondary btn-lg btn-block" name="borrar">Borrar</button>
</button>
</div>
</form>
</div>
</body>
</html>
<?php } ?>
session_start();
if(isset($_POST['borrar'])){
$error=false;
//procesamos el form
require_once __DIR__."/../db/conexion.php";
$dni=trim($_POST['dni']);
//borramos el coche
$q="delete from tabla where dni=?";
$stmt=mysqli_stmt_init($llave);
if(mysqli_stmt_prepare($stmt, $q)){
mysqli_stmt_bind_param($stmt, 's', $dni);
mysqli_stmt_execute($stmt);
}else{
die("Error al borrar");
}
mysqli_stmt_close($stmt);
mysqli_close($llave);
$_SESSION['mensaje']="User eliminado con éxito";
header("Location:main.php");
die();
}
else{
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>Borrar usuario</title>
</head>
<body style="background-color:black">
<h5 class="text-center mt-2">Borrar usuario</h5>
<div class="container">
<form name="aw" action="borrar.php" method="POST" class="text-light">
<div class="mb-3">
<label for="n" class="form-label">dni</label>
<input type="text" class="form-control" id="dni" placeholder="dni" name="dni">
</div>
<button type="submit" class="btn btn-secondary btn-lg btn-block" name="borrar">Borrar</button>
</button>
</div>
</form>
</div>
</body>
</html>
<?php }; ?>
- usuarios.php
$usuarios=[
'miguel'=>["blucod123", "Admin"],
'pepito'=>["pepito123", "Normal"],
'juanito'=>["juanito123", "Gestor"]
];
- login.php
<?php
session_start();
require "usuarios.php";
if(isset($_POST['btn'])){
$error=false;
$nombre=trim($_POST['nombre']);
$pass=$_POST['pass'];
if($error==true){
header("Location:{$_SERVER['PHP_SELF']}");
}
foreach($usuarios as $k=>$v){
if($k==$nombre && $pass==$v[0]){
$_SESSION['username']=$nombre;
$_SESSION['perfil']=$v[1];
header("Location:menu.php");
die();
}
}
//Validación incorrecta
$_SESSION['errValidacion']= "Nombre de usuario o contraseña incorrecta";
header("Location:{$_SERVER['PHP_SELF']}");
die();
}else{
//pintamos la página
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<form name="a" method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cerramos al final del documento
<?php } ?>
- menu.php
<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location_login.php");
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>menu</title>
</head>
<body style="background-color:aqua">
</br>
</br>
</br>
</br>
</br>
</br>
<div class="container mt-4" align="center">
<?php
if($_SESSION['perfil']=="Admin"){
echo "<a href='main.php' class='btn btn-primary'>Ir a la Página 1</a>";
}
?>
</div>
<div class="container mt-4" align="center">
<a href='login.php' class="btn btn-danger">Cerrar Sesion</a>
</div>
</body>
</html>
- cerrarsesion.php
<?php
session_start();
session_destroy();
header("Location:login.php");
mysql
public
- main.php
session_start();
require_once __DIR__ . "/../db/conexion.php";
$q = "select * from tabla";
$resultado = mysqli_query($llave, $q);
mysqli_close($llave);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>usuarios</title>
</head>
<body>
<table class="table table-dark table-striped mt-5">
<thead>
<tr>
<th scope="col">dni</th>
<th scope="col">nombre</th>
<th scope="col">apellido</th>
</tr>
</thead>
<tbody>
<?php
while ($fila = mysqli_fetch_assoc($resultado)) {
echo <<<TXT
<tr>
<td>{$fila['dni']}</td>
<td>{$fila['nombre']}</td>
<td>{$fila['apellido']}</td>
</tr>
TXT;
}
?>
</tbody>
</table>
<a href="nuevo.php" class="btn btn-primary">Agregar usuario</a>
<br>
<br>
<a href="borrar.php" class="btn btn-danger">Borrar usuario</a>
<br>
<br>
<a href="actualizar.php" class="btn btn-secondary">Actualizar usuario</a>
</body>
</html>
- nuevo.php
session_start();
if(isset($_POST['enviar'])){
$error=false;
//procesamos el form
require_once __DIR__."/../db/conexion.php";
$dni=trim($_POST['dni']);
$nombre=trim($_POST['nombre']);
$apellido=trim($_POST['apellido']);
//guardamos el coche
$q="insert into tabla(dni, nombre, apellido) values(?, ?, ?)";
$stmt=mysqli_stmt_init($llave);
if(mysqli_stmt_prepare($stmt, $q)){
mysqli_stmt_bind_param($stmt, 'sss', $dni, $nombre, $apellido);
mysqli_stmt_execute($stmt);
}else{
die("Error al insertar");
}
mysqli_stmt_close($stmt);
mysqli_close($llave);
$_SESSION['mensaje']="User agregado con éxito";
header("Location:main.php");
die();
}
else{
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="wdnith=device-wdnith, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5dnixbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>Agregar usuario</title>
</head>
<body style="background-color:black">
<h5 class="text-center mt-2">Agregar usuario</h5>
<div class="container">
<form name="aw" action="nuevo.php" method="POST" class="text-light">
<div class="mb-3">
<label for="n" class="form-label">dni</label>
<input type="text" class="form-control" id="dni" placeholder="dni" name="dni">
</div>
<div class="mb-3">
<label for="e" class="form-label">nombre</label>
<input type="text" class="form-control" id="nombre" placeholder="nombre" name="nombre">
</div>
<div class="mb-3">
<label for="e" class="form-label">apellido</label>
<input type="text" class="form-control" id="apellido" placeholder="apellido" name="apellido">
</div>
</div>
<div class="d-flex">
<button type="submit" class="btn btn-primary btn-lg btn-block" name="enviar">Enviar</button>
</button>
<button type="delete" class="btn btn-secondary btn-lg btn-block" name="borrar">Borrar</button>
</button>
</div>
</form>
</div>
</body>
</html>
<?php } ?>
- borrar.php
session_start();
if(isset($_POST['borrar'])){
$error=false;
//procesamos el form
require_once __DIR__."/../db/conexion.php";
$dni=trim($_POST['dni']);
//borramos el coche
$q="delete from tabla where dni=?";
$stmt=mysqli_stmt_init($llave);
if(mysqli_stmt_prepare($stmt, $q)){
mysqli_stmt_bind_param($stmt, 's', $dni);
mysqli_stmt_execute($stmt);
}else{
die("Error al borrar");
}
mysqli_stmt_close($stmt);
mysqli_close($llave);
$_SESSION['mensaje']="User eliminado con éxito";
header("Location:main.php");
die();
}
else{
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>Borrar usuario</title>
</head>
<body style="background-color:black">
<h5 class="text-center mt-2">Borrar usuario</h5>
<div class="container">
<form name="aw" action="borrar.php" method="POST" class="text-light">
<div class="mb-3">
<label for="n" class="form-label">dni</label>
<input type="text" class="form-control" id="dni" placeholder="dni" name="dni">
</div>
<button type="submit" class="btn btn-secondary btn-lg btn-block" name="borrar">Borrar</button>
</button>
</div>
</form>
</div>
</body>
</html>
<?php }; ?>