返回介绍

wp_generate_auth_cookie()

发布于 2017-09-11 11:57:55 字数 3003 浏览 1171 评论 0 收藏 0

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

Generate authentication cookie contents.


description


参数

$user_id

(int) (Required) User ID

$expiration

(int) (Required) The time the cookie expires as a UNIX timestamp.

$scheme

(string) (Optional) The cookie scheme to use: auth, secure_auth, or logged_in

Default value: 'auth'

$token

(string) (Optional) User's session token to use for this cookie

Default value: ''


返回值

(string) Authentication cookie contents. Empty string if user does not exist.


源代码

File: wp-includes/pluggable.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
	 *
	 * @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 );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-session-tokens.php: WP_Session_Tokens::get_instance()
  • wp-includes/pluggable.php: wp_hash()
  • wp-includes/pluggable.php: auth_cookie
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/pluggable.php: wp_set_auth_cookie()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文