I am pretty new to PHP, so debugging isn't really something I am familiar with when it comes to PHP.
I am using php/javascript(ajax) to change a users password for my website. So basically, when I log in and try to change my password. The code breaks at the first echo. So the password that I am entering into the form does not match the password in the database. But, I am using the same hash method and everything. If anyone has any ideas, let me know. Thanks!
if(isset($_POST["u"])) {
$u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
$oldpasshash = md5($_POST["cp"]);
$newpasshash = md5($_POST["cnp"]);
$sql = "SELECT id, username, password FROM users WHERE username='$u' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_id = $row["id"];
$db_username = $row["username"];
$db_password = $row["password"];
if($db_password != $oldpasshash){
echo "no_exist";
exit();
} else {
$sql = "UPDATE users SET password='$newpasshash', WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
}
$sql = "SELECT id, username, password FROM users WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_newpass = $row[3];
if($db_newpass == $newpasshash) {
echo "success";
exit();
} else {
echo "pass_failed";
exit();
}
}
var_dump()andprint_r()to inspect variables at various places. Finally, you have potentially opened yourself up to SQL injection vulnerabilities. Use prepared queries to avoid this problem entirely.md5()to encode passwords. If able to, look into hashing your passwordvar_dump($var);on that variable at that point (just write it in your source code and run it again). That way you slowly pinpoint the spot where your actual code diverges from your expectations and where you need to fix something.