Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. //session_destroy('usuario');
  5. session_start();
  6. session_unset();
  7.  
  8. $user ='';
  9. $pass ='';
  10.  
  11. if (isset($_POST['validar'])) { // controla pulsado del botón validar del formulario
  12.  
  13.  
  14. $user = isset($_POST['user']) ? $_POST['user'] : '';
  15. $pass = isset($_POST['pass']) ? $_POST['pass'] : '';
  16.  
  17. if (empty($user)){
  18. echo "No has introducido usuario <br>";
  19. }
  20. if (empty($pass)){
  21. echo "No has introducido contraseña <br>";
  22. }
  23.  
  24.  
  25. function validar($usuario,$clave){
  26.  
  27.  
  28. $error = '';
  29.  
  30. try {
  31. $conn = new PDO('mysql:host=localhost;dbname=gesventa;charset=utf8','dwes','');
  32. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33.  
  34. $stmt = $conn->prepare("select * from usuarios WHERE usr = :usr AND pass = :pass");
  35. $stmt->execute(array(':usr' => $usuario, ':pass' => $clave));
  36.  
  37. $rows = $stmt->fetchAll();
  38. $stmt->closeCursor();
  39.  
  40. if ( sizeof($rows) == 0 )
  41. $error = "El usuario o la contraseña no son válidos";
  42.  
  43. return $error;
  44.  
  45. } catch(PDOException $e) {
  46. print "¡Error!: " . $e->getMessage() . "<br/>";
  47. exit;
  48. }
  49.  
  50. }
  51. }
  52. $error = validar_credenciales($usuario, $clave);
  53. if ( $error == "" ) // Todo OK. Ir a la aplicación de operaciones
  54. {
  55. $_SESSION['usuario'] = $usuario;
  56. header('Location: Ejer7b.php');
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. ?>
  66.  
  67.  
  68. <!DOCTYPE html>
  69.  
  70. <html>
  71. <head>
  72. <meta charset="UTF-8">
  73. <title>Ejer Examen</title>
  74. </head>
  75. <body>
  76. <form name="prueba" action="" method="POST">
  77.  
  78. <h1>Login</h1>
  79. Usuario: <input type="text" name="user" value="<?php echo $user; ?>"><br />
  80. Contraseña: <input type="text" name="pass" value="<?php echo $pass; ?>"><br />
  81. <br>
  82. <input type="submit" name="validar" id="validar">
  83.  
  84.  
  85. </form>
  86. </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement