-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_password_script.php
59 lines (53 loc) · 2.07 KB
/
change_password_script.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// It will not allow direct access to this file by any user.
if(!isset($_SERVER['HTTP_REFERER'])){
// redirect them to your desired location
header('location:index.php');
exit;
}
require "includes/common.php";
$oldPassword = $_POST['oldPassword'];
$newPassword = $_POST['newPassword'];
$confirmNewPassword = $_POST['confirmNewPassword'];
$oldPassword = mysqli_real_escape_string($con, $oldPassword);
$newPassword = mysqli_real_escape_string($con, $newPassword);
$confirmNewPassword = mysqli_real_escape_string($con, $confirmNewPassword);
// Check whether a 2 password matches or not?
if ($newPassword == $confirmNewPassword) {
// Check whether oldPassword is correct or not.
if (checkPassword($oldPassword, $con)) {
changePassword($newPassword, $con);
} else {
// User does not exist or has wrong password.
echo "<script>alert('Please enter correct Password')</script>";
echo "<script>location.href='change_password.php'</script>";
}
} else {
// new pass and confirm pass must be same
echo "<script>alert('New Password and Confirm password must be same')</script>";
echo "<script>location.href='change_password.php'</script>";
}
function checkPassword($oldPassword, $con)
{
$oldPassword = md5($oldPassword);
$query = "select * from users where id = {$_SESSION['user_id']} and password = '$oldPassword' ";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$num = mysqli_num_rows($result);
echo "check" . mysqli_affected_rows($con);
if ($num > 0) {
return true;
} else {
return false;
}
}
function changePassword($pass, $con)
{
$pass = md5($pass);
$query = "update users set password = '$pass' where id = {$_SESSION['user_id']}";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
if (mysqli_affected_rows($con) > 0) {
echo "<script>alert('Password Changed Successfully')</script>";
session_destroy();
echo "<script>location.href='index.php'</script>";
}
}