-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_new.php
55 lines (44 loc) · 1.32 KB
/
password_new.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
<?php
include 'includes/session.php';
if(!isset($_GET['code']) OR !isset($_GET['user'])){
header('location: index.php');
exit();
}
$path = 'password_reset.php?code='.$_GET['code'].'&user='.$_GET['user'];
if(isset($_POST['reset'])){
$password = $_POST['password'];
$repassword = $_POST['repassword'];
if($password != $repassword){
$_SESSION['error'] = 'Passwords did not match';
header('location: '.$path);
}
else{
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM users WHERE reset_code=:code AND id=:id");
$stmt->execute(['code'=>$_GET['code'], 'id'=>$_GET['user']]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$password = password_hash($password, PASSWORD_DEFAULT);
try{
$stmt = $conn->prepare("UPDATE users SET password=:password WHERE id=:id");
$stmt->execute(['password'=>$password, 'id'=>$row['id']]);
$_SESSION['success'] = 'Password successfully reset';
header('location: login.php');
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
header('location: '.$path);
}
}
else{
$_SESSION['error'] = 'Code did not match with user';
header('location: '.$path);
}
$pdo->close();
}
}
else{
$_SESSION['error'] = 'Input new password first';
header('location: '.$path);
}
?>