生成身份验证cookie内容。

原型

wp_generate_auth_cookie( int $user_id, int $expiration, string $scheme = 'auth', string $token = '' )

参数

$user_id (int) (Required) 用户身份

$expiration (int) (Required) Cookie作为UNIX时间戳到期的时间。

$scheme (string) (Optional)  要使用的cookie方案:auth,secure_auth或logged_in

$token (string) (Optional) 用户的会话令牌用于此cookie

返回值

(string)  验证cookie内容。如果用户不存在,则为空字符串。

源文件

路径:wp-includes/pluggable.php

<?php
...
function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $token = '' ) {
	$user = get_userdata($user_id);
	if ( ! $user ) {
		return '';
	}
	if ( ! $token ) {
		$manager = WP_Session_Tokens::get_instance( $user_id );
		$token = $manager->create( $expiration );
	}
	$pass_frag = substr($user->user_pass, 8, 4);
	$key = wp_hash( $user->user_login . '|' . $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, $user->user_login . '|' . $expiration . '|' . $token, $key );
	$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
	/**
	 * Filters the authentication cookie.
	 *
	 * @since 2.5.0
	 * @since 4.0.0 The `$token` parameter was added.
	 *
	 * @param string $cookie     Authentication cookie.
	 * @param int    $user_id    User ID.
	 * @param int    $expiration The time the cookie expires as a UNIX timestamp.
	 * @param string $scheme     Cookie scheme used. Accepts 'auth', 'secure_auth', or 'logged_in'.
	 * @param string $token      User's session token used.
	 */
	return apply_filters( 'auth_cookie', $cookie, $user_id, $expiration, $scheme, $token );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_generate_auth_cookie/