From d86dc8deeacc5320559367e29fceb236c7d14f72 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 28 Jan 2025 11:33:53 +0000 Subject: [PATCH 1/3] Setting up GitHub Classroom Feedback From c6a094148013d79fa6e6041ce98d58d045396c3f Mon Sep 17 00:00:00 2001 From: avilrod3004 Date: Mon, 3 Feb 2025 08:43:38 +0100 Subject: [PATCH 2/3] funciona login de usuario --- docker-compose.yml | 37 +++++++++++++++++++++++++++++++++++++ src/check_user.php | 20 ++++++++++++++++++++ src/index.php | 22 ++++++++++++++++++++++ src/login.php | 37 +++++++++++++++++++++++++++++++++++++ src/model.php | 30 ++++++++++++++++++++++++++++++ src/pdo.php | 18 ++++++++++++++++++ src/perfil.php | 40 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 204 insertions(+) create mode 100644 docker-compose.yml create mode 100644 src/check_user.php create mode 100644 src/index.php create mode 100644 src/login.php create mode 100644 src/model.php create mode 100644 src/pdo.php create mode 100644 src/perfil.php diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d655cf2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: "3.7" + +services: + server: + image: fjortegan/nginx-fpm + ports: + - "8081:80" + volumes: + - ./src:/usr/share/nginx/html + links: + - fpm + + fpm: + image: fjortegan/php-xdebug + volumes: + - ./src:/var/www/html + # Sólo en máquinas Linux, comentar estas dos líneas en otros sistemas de lusers + extra_hosts: + - "host.docker.internal:host-gateway" + + db: + image: mariadb + #ports: + # - "3306:3306" + volumes: + - ./db-data:/var/lib/mysql/ + environment: + MYSQL_ROOT_PASSWORD: pestillo + MYSQL_USER: usuario + MYSQL_PASSWORD: cerrojo + + phpmyadmin: + image: phpmyadmin + ports: + - "8080:80" + environment: + - PMA_ARBITRARY=1 diff --git a/src/check_user.php b/src/check_user.php new file mode 100644 index 0000000..baf93cf --- /dev/null +++ b/src/check_user.php @@ -0,0 +1,20 @@ +prepare("SELECT username, password FROM usuarios WHERE username = :username"); +$statement->execute(array(":username" => $username)); + +$resultado = $statement->fetchObject(); +$veri=password_verify($password, $resultado->password); + +if($veri) { + session_start(); + $_SESSION["username"] = $_POST["username"]; + session_write_close(); + header("Location: lista.php"); +} else { + header("Location: login.php?error=Usuario y/o clave incorrectos"); +} diff --git a/src/index.php b/src/index.php new file mode 100644 index 0000000..d87c881 --- /dev/null +++ b/src/index.php @@ -0,0 +1,22 @@ + + + + + + + Quizzes App + + +
+

Quizzes App

+ + +
+ + diff --git a/src/login.php b/src/login.php new file mode 100644 index 0000000..b72918c --- /dev/null +++ b/src/login.php @@ -0,0 +1,37 @@ + + + + + + + Login + + +
+ +
+ +
+
+ + + + + + + +
+ + ' . $_GET["error"] . ""; + } + ?> +
+ + + + diff --git a/src/model.php b/src/model.php new file mode 100644 index 0000000..36c80fc --- /dev/null +++ b/src/model.php @@ -0,0 +1,30 @@ +conn = new PDO("mysql:host=db;dbname=quizzes", "root", "pestillo"); + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + public static function getInstance() { + if(!isset(self::$instance)) { + self::$instance = new Model(); + } + return self::$instance; + } + + public function crea_usuario($username, $password, $image = "anon.png") { + $statement = $this->conn->prepare("INSERT INTO usuarios (username, password) VALUES (:username, :password)"); + $statement->execute(array(":username" => $username, ":password" => crypt($password, "juas"))); + return $statement->rowCount(); + } + + public function check_user($username, $password) { + $statement = $this->conn->prepare("SELECT count(*) FROM usuarios WHERE username = :username AND password = :password"); + $statement->execute(array(":username" => $username, ":password" => crypt($password, "juas"))); + return $statement->fetch()[0] == 1; + } +} diff --git a/src/pdo.php b/src/pdo.php new file mode 100644 index 0000000..d1615c7 --- /dev/null +++ b/src/pdo.php @@ -0,0 +1,18 @@ + false, // turn off emulation mode for "real" prepared statements + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array + ]; + + $conn=new PDO("mysql:host=$servidor;dbname=$bd;charset=utf8",$usuario,$clave,$options); +} catch(PDOException $e) { + echo "Error de conexión" . $e->getMessage(); +} diff --git a/src/perfil.php b/src/perfil.php new file mode 100644 index 0000000..a070608 --- /dev/null +++ b/src/perfil.php @@ -0,0 +1,40 @@ + + + + + + + + + Perfil + + +
+

Quizzes

+
+ +
+ + +
+

Listado quizzes

+
+
+ + + + From 3ca71bb3b6e28eb2417609decf1b050db59391ac Mon Sep 17 00:00:00 2001 From: avilrod3004 Date: Mon, 3 Feb 2025 08:54:04 +0100 Subject: [PATCH 3/3] cambio rutas --- src/{ => db}/model.php | 0 src/{ => db}/pdo.php | 0 src/index.php | 4 ++-- src/quizz/crear_quizz.php | 3 +++ src/{ => user}/check_user.php | 6 +++--- src/user/create_user.php | 26 ++++++++++++++++++++++++ src/{ => user}/login.php | 0 src/{ => user}/perfil.php | 2 +- src/user/register.php | 37 +++++++++++++++++++++++++++++++++++ 9 files changed, 72 insertions(+), 6 deletions(-) rename src/{ => db}/model.php (100%) rename src/{ => db}/pdo.php (100%) create mode 100644 src/quizz/crear_quizz.php rename src/{ => user}/check_user.php (85%) create mode 100644 src/user/create_user.php rename src/{ => user}/login.php (100%) rename src/{ => user}/perfil.php (92%) create mode 100644 src/user/register.php diff --git a/src/model.php b/src/db/model.php similarity index 100% rename from src/model.php rename to src/db/model.php diff --git a/src/pdo.php b/src/db/pdo.php similarity index 100% rename from src/pdo.php rename to src/db/pdo.php diff --git a/src/index.php b/src/index.php index d87c881..acd6920 100644 --- a/src/index.php +++ b/src/index.php @@ -13,8 +13,8 @@ diff --git a/src/quizz/crear_quizz.php b/src/quizz/crear_quizz.php new file mode 100644 index 0000000..ae762c1 --- /dev/null +++ b/src/quizz/crear_quizz.php @@ -0,0 +1,3 @@ +fetchObject(); $veri=password_verify($password, $resultado->password); -if($veri) { +if ($veri) { session_start(); $_SESSION["username"] = $_POST["username"]; session_write_close(); - header("Location: lista.php"); + header("Location: /user/perfil.php"); } else { header("Location: login.php?error=Usuario y/o clave incorrectos"); } diff --git a/src/user/create_user.php b/src/user/create_user.php new file mode 100644 index 0000000..13712b2 --- /dev/null +++ b/src/user/create_user.php @@ -0,0 +1,26 @@ + + + +check_user($username, $password)) + $resultado=$my_model->crea_usuario($username, $password); + +if($resultado) { + session_start(); + $_SESSION["username"] = $_POST["username"]; + session_write_close(); + header("Location: login.php"); +} else { + header("Location: login.php?error=No se ha podido crear el usuario"); +} diff --git a/src/login.php b/src/user/login.php similarity index 100% rename from src/login.php rename to src/user/login.php diff --git a/src/perfil.php b/src/user/perfil.php similarity index 92% rename from src/perfil.php rename to src/user/perfil.php index a070608..48ed384 100644 --- a/src/perfil.php +++ b/src/user/perfil.php @@ -23,7 +23,7 @@ diff --git a/src/user/register.php b/src/user/register.php new file mode 100644 index 0000000..66e29da --- /dev/null +++ b/src/user/register.php @@ -0,0 +1,37 @@ + + + + + + + Registro de usuarios + + +
+ +
+ +
+
+ + + + + + + +
+ + ' . $_GET["error"] . ""; + } + ?> +
+ + + +