wp_validate_auth_cookie( string $cookie = '', string $scheme = '' ): int|false
- Since
- 2.5.0
- Source
wp-includes/pluggable.php:693
Description
The checks include making sure that the authentication cookie is set and pulling in the contents (if $cookie is not used).
Makes sure the cookie is not expired. Verifies the hash in cookie is what is should be and compares the two.
Compatibility
- WordPress
- since 2.5.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
$cookiestringoptional- If used, will validate contents instead of cookie's.Default:
'' $schemestringoptional- The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.Default:
''
Return value
int|false- User ID if valid cookie, false if invalid.
Performance profile
How much work a call to wp_validate_auth_cookie() 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
- 14–86
- Plugin surface
- 6 hooks
- Called by
- 3
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 112.
Third-party callbacks on 'auth_cookie_malformed', 'auth_cookie_expired', 'auth_cookie_bad_username' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - querycontent query
get_user_by()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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_validate_auth_cookie() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14 | wp_parse_auth_cookie(), do_action() |
$expired | 29–33 | wp_parse_auth_cookie(), wp_doing_ajax(), time(), do_action() |
!$expired | 35–39 | wp_parse_auth_cookie(), wp_doing_ajax(), time(), get_user_by(), do_action() |
!$expired && !hash_equals() | 64–68 | wp_parse_auth_cookie(), wp_doing_ajax(), time(), get_user_by(), wp_hash(), hash_hmac(), hash_equals(), do_action() |
!$expired && hash_equals() && !->verify() | 74–78 | wp_parse_auth_cookie(), wp_doing_ajax(), time(), get_user_by(), wp_hash(), hash_hmac(), hash_equals(), ::WP_Session_Tokens(), ->verify(), do_action() |
!$expired && hash_equals() && ->verify() | 80–86 | wp_parse_auth_cookie(), wp_doing_ajax(), time(), get_user_by(), wp_hash(), hash_hmac(), hash_equals(), ::WP_Session_Tokens(), ->verify(), time(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 112 | 14–86 | 8 | |
| 8.5 | 112 | 14–86 | 8 | |
| 8.4 | 112 | 14–86 | 8 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 116 | 14–90 | 8 | |
| 8.2 | 116 | 14–90 | 8 | |
| 8.1 | 116 | 14–90 | 8 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 117 | 14–91 | 8 |
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 · 6
6 hooks fire while wp_validate_auth_cookie() runs, in this order:
- do_action( auth_cookie_malformed )actionline 705 (+12 into the body)
Fires if an authentication cookie is malformed.
- do_action( auth_cookie_expired )actionline 739 (+46 into the body)
Fires once an authentication cookie has expired.
- do_action( auth_cookie_bad_username )actionline 761 (+68 into the body)
Fires if a bad username is entered in the user authentication process.
- do_action( auth_cookie_bad_hash )actionline 790 (+97 into the body)
Fires if a bad authentication cookie hash is encountered.
- do_action( auth_cookie_bad_session_token )actionline 812 (+119 into the body)
Fires if a bad session token is encountered.
- do_action( auth_cookie_valid )actionline 837 (+144 into the body)
Fires once an authentication cookie has been validated.
Uses · 8
- wp_parse_auth_cookie()Parses a cookie into its components.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_doing_ajax()Determines whether the current request is a WordPress Ajax request.
- get_user_by()Retrieves user info by a given field.
- wp_hash()Gets hash of given string.
- hash_hmac()Compat function to mimic hash_hmac().
- hash_equals()Timing attack safe string comparison.
- WP_Session_Tokens::get_instance()Retrieves a session manager instance for a user.
Used by · 3
- auth_redirect()Checks if a user is logged in, if not it redirects them to the login page.
- wp_authenticate_cookie()Authenticates the user using the WordPress auth cookie.
- wp_validate_logged_in_cookie()Validates the logged-in cookie.
Source code
function wp_validate_auth_cookie( $cookie = '', $scheme = '' ) { $cookie_elements = wp_parse_auth_cookie( $cookie, $scheme ); if ( ! $cookie_elements ) { /** * Fires if an authentication cookie is malformed. * * @since 2.7.0 * * @param string $cookie Malformed auth cookie. * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth', * or 'logged_in'. */ do_action( 'auth_cookie_malformed', $cookie, $scheme ); return false; } $scheme = $cookie_elements['scheme']; $username = $cookie_elements['username']; $hmac = $cookie_elements['hmac']; $token = $cookie_elements['token']; $expired = $cookie_elements['expiration']; $expiration = $cookie_elements['expiration']; // Allow a grace period for POST and Ajax requests. if ( wp_doing_ajax() || 'POST' === $_SERVER['REQUEST_METHOD'] ) { $expired += HOUR_IN_SECONDS; } // Quick check to see if an honest cookie has expired. if ( $expired < time() ) { /** * Fires once an authentication cookie has expired. * * @since 2.7.0 * * @param string[] $cookie_elements { * Authentication cookie components. None of the components should be assumed * to be valid as they come directly from a client-provided cookie value. * * @type string $username User's username. * @type string $expiration The time the cookie expires as a UNIX timestamp. * @type string $token User's session token used. * @type string $hmac The security hash for the cookie. * @type string $scheme The cookie scheme to use. * } */ do_action( 'auth_cookie_expired', $cookie_elements ); return false; } $user = get_user_by( 'login', $username ); if ( ! $user ) { /** * Fires if a bad username is entered in the user authentication process. * * @since 2.7.0 * * @param string[] $cookie_elements { * Authentication cookie components. None of the components should be assumed * to be valid as they come directly from a client-provided cookie value. * * @type string $username User's username. * @type string $expiration The time the cookie expires as a UNIX timestamp. * @type string $token User's session token used. * @type string $hmac The security hash for the cookie. * @type string $scheme The cookie scheme to use. * } */ do_action( 'auth_cookie_bad_username', $cookie_elements ); return false; } $pass_frag = substr( $user->user_pass, 8, 4 ); $key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme ); // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; $hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );Changelog
Introduced in 2.5.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 6.7.7 tag, from
src/wp-includes/pluggable.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.