gpt4 book ai didi

PHP - MYSQL - 如果用户名存在,检查特定用户名的密码

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:43 24 4
gpt4 key购买 nike

我有一个我正在尝试弄清楚的项目的登录名。我通过 POST(通过 AJAX 调用)获得了一个值,我已经检查了输入的用户名是否存在,直到那里,它运行良好。但是知道我想检查密码是否对该用户名有效。这是 PHP 代码:

<?php

//File with the conection data
include_once "../conexion.php";

//Initialization
$user = "";
$password = "";
$errors = "";
$result = "";
$result2 = "";

//Some validations (I've edited a little to make it shorter)
if((isset($_POST['user'])) && (!empty($_POST['user']))){
$user = $_POST['user'];
}else{
$errors .= "blablablah";
}

if((isset($_POST['password'])) && (!empty($_POST['password']))){
$password = $_POST['password'];
}else{
$errors .= "blablabla";
}

//I make the query
$sql = "SELECT user FROM users WHERE user = ?";

//I prepare the query
if($stmt = $con->prepare($sql)){

$stmt->bind_param('s', $user);
$result = $stmt->execute();

}

/* UP TO HERE, if I check that $result is true and echo "User exists" or something like that, IT WORKS, AS THE USER EXISTS */

/* BUT now I want to check the PASSWORD, given that the user exists */

if($result){

//I make the query
$sql2 = "SELECT user, password FROM users WHERE user = ? AND password = ?";

//I prepare the query
if($stmt2 = $con->prepare($sql2)){

$stmt2->bind_param('ss', $user, $password);
$result2 = $stmt2->execute();


if($result2){
echo "ENTERED";
}else{
echo "PASSWORD OR USER INCORRECT";
}
}

}

?>

我在 AJAX 调用的成功函数中使用了这些 echo 的结果,这是相关代码(表单按钮中有一个 onClick 事件 (onClick="login()"),而 validationLogin( ) 具有字段的所有验证 --> 所有工作正常):

function login(){

if(validationLogin()){
$.ajax({

url: "http://localhost/myProject/extras/Login.php",
type: "POST",
data: {"user": user,
"password": password,
},
dataType: "html",
cache: false,
beforeSend: function() {
console.log("Processing...");
},
success:
function(data){

alert(data);
console.log(data);

}

});

}else{
//alert("Incorrect fields");
}

}

这返回 EMPTY,我警告数据只是为了检查它有什么...警告是空的,不明白为什么:/

我试过这个想法 --> PHP mySQL check if username and password are in the database但在那种情况下,它一直说这是不正确的:/

一些注意事项:

  • 我知道密码应该加密,以后可能会使用 md5。
  • 通过使用 PHP 文件中的 echo 和 JS 文件中的 alert(data)/console.log(data),我只想检查是否有效,以便继续。也许还有其他方法,更好的方法来做这一切,我知道,但我喜欢一点一点地进行
  • 我真的很想理解我编写的代码,然后对其进行改进,我真的很想了解它的工作原理和原因
  • 我想继续使用准备好的语句

先谢谢大家了! :)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com