Ayuda validar login
Publicado por Henry (11 intervenciones) el 23/02/2021 20:20:04
Buenas tardes tengo una buena duda acerca de mi codificación sobre validar un login. El detalle es que funciona tan bien que inicia sesión incluso aunque no este en la base de datos. Alguien podría asesorarme sobre que estoy haciendo mal? Estoy trabajando con Jquery mobile (estoy casi obligado a usarlo).
Login
JS
PHP
Login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
include 'conexion.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">
<link rel="stylesheet" href="Jquery/jquery.mobile-1.4.5.css">
<script src="Jquery/jquery.min.js"></script>
<script src="Jquery/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="js/login.js"></script>
<title>Inicio de sesión</title>
</head>
<body>
<div data-role="page" id="login">
<div data-role="header" data-theme="b">
<a href="index.php" data-role="button" data-icon="home" data-theme="b">Regresar</a>
<h1>Inicio de sesión</h1>
</div>
<div class="ui-content" role="main">
<!-- action="validar_login.php" method="post" data-ajax="false" -->
<form id="form">
<label for="basic">Usuario</label>
<input type="text" name="username" id="username" data-mini="true">
<label for="basic">Contraseña</label>
<input type="password" name="password" id="password" data-mini="true">
<p>
<input type="button" name="ingresar" value="ingresar" id="ingresar">
</form>
<a href="registro.php" data-transition="slide" >¿No tiene cuenta? Registrese</a>
</div>
<div data-role="footer" data-theme="b" data-position="fixed">
<h2>Copyright 2021</h2>
</div>
</div>
</body>
</html>
JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//$(document).ready(function(){
$(document).on("pagecreate", "#login", function(){
$("#ingresar").click(function(){
var username = $("#username").val();
var password = $("#password").val();
if (username.length == " " || password.length == '')
{
alert('Introduce correctamente los datos')
return false;
}
else
{
$.ajax({
url: 'validar_login.php',
type: 'POST',
data:{
username : username, password : password,
},
})
.done(function(){
console.log("success");
$.mobile.changePage("index.php");
})
.fail(function(){
console.log("error");
alert("No se pudo conectar con la BD")
})
.always(function(){
console.log("complete");
})
}
});
});
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
session_start();
require_once("conexion.php");
global $link;
$_SESSION['username']=$_POST['username'];
$_SESSION['password']=$_POST['password'];
$sql="SELECT * FROM usuarios where username ='$_SESSION[username]' and password= '$_SESSION[password]'";
$result=mysqli_query($link, $sql) or die(mysqli_error());
$rows=mysqli_num_rows($result);
if($rows){ // nos devuelve 1 si encontro el usuario y el password
return 1;
}
else
{
return 0;
}
mysqli_close($link);
?>
Valora esta pregunta


0