简短介绍
快速查看WP源代码后,我想我找到了解决方案。。。
WordPress使用两个函数来设置和解析身份验证Cookie:
wp_generate_auth_cookie
wp_parse_auth_cookie
中有一个过滤器
wp_generate_auth_cookie
调用
auth_cookie
您可能可以使用它来更改cookie的内容,但里面没有过滤器
wp_parse_auth_cookie
, 但是
这两个函数都是在pluggable中定义的。php,这意味着您可以为它们编写自己的实现并覆盖默认的实现。
解决方案
- 编写自己的插件(我们称之为更好的验证Cookie)
- 实现自己的插件
wp_generate_auth_cookie
和wp_parse_auth_cookie
此插件中的函数激活你的插件
您可以在下面找到这些函数的示例实现(主要基于原始版本):
if ( !function_exists(\'wp_generate_auth_cookie\') ) :
/**
* Generate authentication cookie contents.
*
* @since 2.5.0
*
* @param int $user_id User ID
* @param int $expiration Cookie expiration in seconds
* @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
* @param string $token User\'s session token to use for this cookie
* @return string Authentication cookie contents. Empty string if user does not exist.
*/
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_id . \'|\' . $expiration . \'|\' . $token . \'|\' . $hash;
/**
* Filter the authentication cookie.
*
* @since 2.5.0
*
* @param string $cookie Authentication cookie.
* @param int $user_id User ID.
* @param int $expiration Authentication cookie expiration in seconds.
* @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 );
}
endif;
if ( !function_exists(\'wp_parse_auth_cookie\') ) :
/**
* Parse a cookie into its components
*
* @since 2.7.0
*
* @param string $cookie
* @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
* @return array Authentication cookie components
*/
function wp_parse_auth_cookie($cookie = \'\', $scheme = \'\') {
if ( empty($cookie) ) {
switch ($scheme){
case \'auth\':
$cookie_name = AUTH_COOKIE;
break;
case \'secure_auth\':
$cookie_name = SECURE_AUTH_COOKIE;
break;
case "logged_in":
$cookie_name = LOGGED_IN_COOKIE;
break;
default:
if ( is_ssl() ) {
$cookie_name = SECURE_AUTH_COOKIE;
$scheme = \'secure_auth\';
} else {
$cookie_name = AUTH_COOKIE;
$scheme = \'auth\';
}
}
if ( empty($_COOKIE[$cookie_name]) )
return false;
$cookie = $_COOKIE[$cookie_name];
}
$cookie_elements = explode(\'|\', $cookie);
if ( count( $cookie_elements ) !== 4 ) {
return false;
}
list( $user_id, $expiration, $token, $hmac ) = $cookie_elements;
$user = get_userdata($user_id);
$username = ( ! $user ) ? \'\' : $user->user_login;
return compact( \'username\', \'expiration\', \'token\', \'hmac\', \'scheme\' );
}
endif;
这些函数的我的版本将替换
user_login
具有
user_id
. 但这应该是一个很好的开始,可以将其更改为更复杂的内容(即特定于用户的哈希或类似的内容)。