check_password_reset_key( string $key, string $login ): WP_User|WP_Error
- Since
- 3.1.0
- Source
wp-includes/user.php:3176
Description
A key is considered 'expired' if it exactly matches the value of the user_activation_key field, rather than being matched after going through the hashing process. This field is now hashed; old values are no longer accepted but have a different WP_Error code so good user feedback can be provided.
Compatibility
- WordPress
- since 3.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$keystring- The password reset key.
$loginstring- The user login.
Return value
WP_User|WP_Error- WP_User object on success, WP_Error object for invalid or expired keys.
Performance profile
How much work a call to check_password_reset_key() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 15–76
- Plugin surface
- 2 hooks
- Called by
- 0
Reaches the database via get_user_by().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 127.
Third-party callbacks on 'password_reset_expiration', 'password_reset_key_expired' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_user_by()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost check_password_reset_key() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–21 | __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) | 27 | get_user_by(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && !$user | 39 | get_user_by(), apply_filters(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && $expiration_time | 43 | get_user_by(), apply_filters(), wp_verify_fast_hash(), time() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user | 48 | get_user_by(), apply_filters(), wp_verify_fast_hash(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && !$user | 48 | get_user_by(), apply_filters(), explode(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !hash_equals() | 52–56 | get_user_by(), apply_filters(), wp_verify_fast_hash(), hash_equals(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time | 52 | get_user_by(), apply_filters(), wp_verify_fast_hash(), time(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && $expiration_time | 52 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user | 57 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user | 58–63 | get_user_by(), apply_filters(), wp_verify_fast_hash(), hash_equals(), __(), apply_filters() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time && !hash_equals() | 58–60 | get_user_by(), apply_filters(), wp_verify_fast_hash(), time(), hash_equals(), __() |
6 further outcomes, up to 76 instructions
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !hash_equals() | 61–65 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), hash_equals(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time | 61 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time | 64–67 | get_user_by(), apply_filters(), wp_verify_fast_hash(), time(), hash_equals(), __(), apply_filters() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user | 67–72 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), hash_equals(), __(), apply_filters() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time && !hash_equals() | 67–69 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time(), hash_equals(), __() |
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time | 73–76 | get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time(), hash_equals(), __(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 127 | 15–76 | 15 | |
| 8.5 | 127 | 15–76 | 15 | |
| 8.4 | 127 | 15–76 | 15 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 133 | 18–82 | 15 | |
| 8.2 | 133 | 18–82 | 15 | |
| 8.1 | 133 | 18–82 | 15 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 134 | 18–83 | 15 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while check_password_reset_key() runs, in this order:
- apply_filters( password_reset_expiration )filterline 3204 (+28 into the body)
Filters the expiration time of password reset keys.
- apply_filters( password_reset_key_expired )filterline 3244 (+68 into the body)
Filters the return value of check_password_reset_key() when an old-style key or an expired key is used.
Uses · 6
- __()Retrieves the translation of $text.
- get_user_by()Retrieves user info by a given field.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_verify_fast_hash()Checks whether a plaintext message matches the hashed value. Used to verify values hashed via wp_fast_hash().
- WP_Error::__construct()Initializes the error.
Source code
function check_password_reset_key( #[\SensitiveParameter] $key, $login) { $key = preg_replace( '/[^a-z0-9]/i', '', $key ); if ( empty( $key ) || ! is_string( $key ) ) { return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); } if ( empty( $login ) || ! is_string( $login ) ) { return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); } $user = get_user_by( 'login', $login ); if ( ! $user ) { return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); } /** * Filters the expiration time of password reset keys. * * @since 4.3.0 * * @param int $expiration The expiration time in seconds. */ $expiration_duration = apply_filters( 'password_reset_expiration', DAY_IN_SECONDS ); if ( str_contains( $user->user_activation_key, ':' ) ) { list( $pass_request_time, $pass_key ) = explode( ':', $user->user_activation_key, 2 ); $expiration_time = $pass_request_time + $expiration_duration; } else { $pass_key = $user->user_activation_key; $expiration_time = false; } if ( ! $pass_key ) { return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); } $hash_is_correct = wp_verify_fast_hash( $key, $pass_key ); if ( $hash_is_correct && $expiration_time && time() < $expiration_time ) { return $user; } elseif ( $hash_is_correct && $expiration_time ) { // Key has an expiration time that's passed. return new WP_Error( 'expired_key', __( 'Invalid key.' ) ); } if ( hash_equals( $user->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) { $return = new WP_Error( 'expired_key', __( 'Invalid key.' ) ); $user_id = $user->ID; /** * Filters the return value of check_password_reset_key() when an * old-style key or an expired key is used. * * Prior to 3.7, plain-text keys were stored in the database. * * @since 3.7.0 * @since 4.3.0 Previously key hashes were stored without an expiration time. * * @param WP_Error $return A WP_Error object denoting an expired key. * Return a WP_User object to validate the key. * @param int $user_id The matched user ID. */ return apply_filters( 'password_reset_key_expired', $return, $user_id ); } return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );}Changelog
Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.