1
+ <?php
2
+
3
+ $ token = $ _POST ["token " ];
4
+
5
+ $ token_hash = hash ("sha256 " , $ token );
6
+
7
+ $ mysqli = require __DIR__ . "/database.php " ;
8
+
9
+ $ sql = "SELECT * FROM user
10
+ WHERE reset_token_hash = ? " ;
11
+
12
+ $ stmt = $ mysqli ->prepare ($ sql );
13
+
14
+ $ stmt ->bind_param ("s " , $ token_hash );
15
+
16
+ $ stmt ->execute ();
17
+
18
+ $ result = $ stmt ->get_result ();
19
+
20
+ $ user = $ result ->fetch_assoc ();
21
+
22
+ if ($ user === null ) {
23
+ die ("token not found " );
24
+ }
25
+
26
+ if (strtotime ($ user ["reset_token_expires_at " ]) <= time ()) {
27
+ die ("token has expired " );
28
+ }
29
+
30
+ if (strlen ($ _POST ["password " ]) < 8 ) {
31
+ die ("Password must be at least 8 characters " );
32
+ }
33
+
34
+ if ( ! preg_match ("/[a-z]/i " , $ _POST ["password " ])) {
35
+ die ("Password must contain at least one letter " );
36
+ }
37
+
38
+ if ( ! preg_match ("/[0-9]/ " , $ _POST ["password " ])) {
39
+ die ("Password must contain at least one number " );
40
+ }
41
+
42
+ if ($ _POST ["password " ] !== $ _POST ["password_confirmation " ]) {
43
+ die ("Passwords must match " );
44
+ }
45
+
46
+ $ password_hash = password_hash ($ _POST ["password " ], PASSWORD_DEFAULT );
47
+
48
+ $ sql = "UPDATE user
49
+ SET password_hash = ?,
50
+ reset_token_hash = NULL,
51
+ reset_token_expires_at = NULL
52
+ WHERE id = ? " ;
53
+
54
+ $ stmt = $ mysqli ->prepare ($ sql );
55
+
56
+ $ stmt ->bind_param ("ss " , $ password_hash , $ user ["id " ]);
57
+
58
+ $ stmt ->execute ();
59
+
60
+ echo "Password updated. You can now login. " ;
0 commit comments