wppaste
WordPress

wp_validate_auth_cookie( string $cookie = '', string $scheme = '' ): int|false

Since
2.5.0
Source
wp-includes/pluggable.php:782
Validates authentication cookie.

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

$schemestringoptional
The cookie scheme to use: 'auth', 'secure_auth', or 'logged_in'.
Note: This does not default to 'auth' like other cookie functions.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

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–93

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 121.

Plugin surface
6 hooks

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.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • querycontent queryget_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.

WhenInstructionsCalls it makes
always14wp_parse_auth_cookie(), do_action()
$expired29–33wp_parse_auth_cookie(), wp_doing_ajax(), time(), do_action()
!$expired35–39wp_parse_auth_cookie(), wp_doing_ajax(), time(), get_user_by(), do_action()
!$expired && !hash_equals()68–75wp_parse_auth_cookie(), wp_doing_ajax(), time(), get_user_by(), wp_hash(), hash_hmac(), hash_equals(), do_action()
!$expired && hash_equals() && !->verify()78–85wp_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()84–93wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev12114–9310
8.512114–9310
8.412114–931014 fewer instructions than PHP 8.3
8.313514–10310
8.213514–10310
8.113514–103103 fewer instructions than PHP 7.4
7.413814–10610

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:

  1. do_action( auth_cookie_malformed )actionline 794 (+12 into the body)

    Fires if an authentication cookie is malformed.

  2. do_action( auth_cookie_expired )actionline 829 (+47 into the body)

    Fires once an authentication cookie has expired.

  3. do_action( auth_cookie_bad_username )actionline 851 (+69 into the body)

    Fires if a bad username is entered in the user authentication process.

  4. do_action( auth_cookie_bad_hash )actionline 884 (+102 into the body)

    Fires if a bad authentication cookie hash is encountered.

  5. do_action( auth_cookie_bad_session_token )actionline 906 (+124 into the body)

    Fires if a bad session token is encountered.

  6. do_action( auth_cookie_valid )actionline 931 (+149 into the body)

    Fires once an authentication cookie has been validated.

Uses · 7

Used by · 3

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'];		$expiration = $cookie_elements['expiration']; 		$expired = (int) $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;		} 		if ( str_starts_with( $user->user_pass, '$P$' ) || str_starts_with( $user->user_pass, '$2y$' ) ) {			// Retain previous behaviour of phpass or vanilla bcrypt hashed passwords.			$pass_frag = substr( $user->user_pass, 8, 4 );		} else {			// Otherwise, use a substring from the end of the hash to avoid dealing with potentially long hash prefixes.			$pass_frag = substr( $user->user_pass, -4 );		}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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/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.