UT5: DESARROLLO DE APLICACIONES WEB

Alberto Méndez Núñez | 03/10/2025

Curso 2025/2026 - Grupo DAW2

Muestra del código PHP ejercicio 2

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>CFGS - Desarrollo de Aplicaciones Web</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f9;
            margin: 0;
            padding: 0;
        }
        header {
            background: #F59C27;
            color: white;
            padding: 15px;
            text-align: center;
        }
        h1 {
            margin: 0;
        }
        main {
            margin: 0 auto;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);      
        }
        ul {
            list-style: none;
            padding: 0;
        }
        footer{
            margin: auto;
            background-color: #F59C27;
            text-align: center;
            height: 150px;
        color: white;
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
        }
        a{
            text-decoration: none;
            color:purple;
        }
        
        table{
            border-collapse: collapse;
            width: 100%;
            border-width: 4px;
        }
        
        td{
            padding: 10px;
            border-width: 4px;
        }
        
        #encabezado{
            background-color: lightsteelblue;
            font-weight: bold;
        }
        
        .codigos{
            background-color: lightblue;
        }
        
        .mostrar{
            background-color: lightsalmon;
        }
        
        tr{
            height: 40px;
        }
        
        tr:nth-child(1){
            background-color: lightblue;
        }
        
        table{
            width: 800px;
        }
        
        td{
            border: 1px solid black;
        }

    </style>
</head>
<body>
    <header>
        <h1><b>UT5: DESARROLLO DE APLICACIONES WEB</b></h1>
        <h4><a href="../../AMNDWESProyectoDWES/indexProyectoDWES.php">Alberto Méndez Núñez | 03/10/2025</a></h4>
        <p>Curso 2025/2026 - Grupo DAW2</p>
    </header>
    <main>
        <h2><b>Ejercicio 2</b></h2>
        
        <?php
         /*
         * Autor: Alberto Méndez Núñez
         * Fecha de ultima modificación: 18/11/2025
         * Desarrollo de un control de acceso con identificación del usuario basado en la función header() y en el uso de una tabla “Usuario” de la base de datos. (PDO).
         */
        require_once '../config/confDBPDO.php';
        
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
            header('WWW-Authenticate: Basic Realm="Contenido restringido"');
            header('HTTP/1.0 401 Unauthorized');
            echo "Introduce tus credenciales";
            exit;
            }

            $usuario = $_SERVER['PHP_AUTH_USER'];
            $passwd  = $_SERVER['PHP_AUTH_PW'];

            try {
                $miDB = new PDO(RUTA, USUARIO, PASS);
                $miDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $query = "SELECT T01_CodUsuario, T01_Password, T01_DescUsuario 
                          FROM T01_Usuario 
                          WHERE T01_CodUsuario = :usuario 
                          AND T01_Password = SHA2(:passwd, 256)";

                $resultado = $miDB->prepare($query);
                $resultado->execute([
                    ':usuario' => $usuario,
                    ':passwd'  => $passwd
                ]);

                $usuarioBD = $resultado->fetch(PDO::FETCH_ASSOC);

                if (!$usuarioBD) {
                    header('WWW-Authenticate: Basic Realm="Contenido restringido"');
                    header('HTTP/1.0 401 Unauthorized');
                    echo "Credenciales incorrectas!";
                    exit;
                }

            } catch (Exception $ex) {
                echo "Error: " . $ex->getMessage();
                exit;
            }

            ?>

            <section>
                <h2>Bienvenido <?php echo $usuarioBD['T01_DescUsuario']; ?></h2>
                <h2>Sesión iniciada correctamente.</h2>
            </section>    
    </main>
</body>
</html>