I need to create a PHP page to allow users to change passwords using their usern
ID: 3862358 • Letter: I
Question
I need to create a PHP page to allow users to change passwords using their username (I want to avoid sql injection). Also, I want to be able to salt the password. Could you help me to fix any mistakes in this code and help me to salt it? thanks in advance.
I need to create a PHP page to allow users to change passwords using their username (I want to avoid sql injection). Also, I want to be able to salt the password. Could you help me to fix any mistakes in this code and help me to salt it? thanks in advance.
Explanation / Answer
function filterr ($ string) {
if (! get_magic_quotes_gpc ()) {
return mysql_real_escape_string ($ string);
} Else
return mysql_real_escape_string (stripslashes ($ string));
}
if (isset ($ _ POST [ 'Submit'])) {
if (! empty ($ _ POST [ 'username']) &&! empty ($ _ POST [ 'password']) &&! empty ($ _ POST [ 'newpassword']) &&! empty ($ _ POST [ 'confirmnewpassword'])) {
$ Username = filterr ($ _ POST [ 'username']);
$ Password = md5 (filterr ($ _ POST [ 'password']));
$ Sql = mysql_query ( "SELECT` password` FROM `users` WHERE` username` = '$ username' AND `password` = '$ password'") or die (mysql_error ());
if (mysql_num_rows ($ sql)> 0) {
$ Newpassword = filterr ($ _ POST [ 'newpassword']);
$ Confirmnewpassword = filterr ($ _ POST [ 'confirmnewpassword']);
if (strcasecmp ($ newpassword, $ confirmnewpassword) == 0) {
$ Update = mysql_query ( "update` users` set `password` = '". Md5 ($ newpassword). "' Where` username` = '$ username' ") or die (mysql_error ());
if ($ update)
$ Prompt = "Password changed";
} Else
$ Prompt = "new password and repeat it are not the same";
} Else
$ Prompt = "Your current password is not correct";
} Else
$ Prompt = "Please complete the form and fill";
}
echo (isset ($ prompt))? $ prompt: "";
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.