问题描述

WordPress 不使用会话。

我一直想知道当用户从一个页面到另一个页面时,WP 使用什么机制来维护用户状态?

最佳解决方案

它使用裸露的 cookie 并存储登录状态信息客户端。

+

=

wordpress_7339a175323c25a8547b5a6d26c49afa=yourusername%7C1457109155%7C170f103ef3dc57cdb1835662d97c1e13;

所有这些饼干和盐从哪里来?

盐在你的 wp-config.php 文件中:

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

独特的短语用于 cryptographic hash function

认证 cookie,其名称存储在 AUTH_COOKIE 内,通过将 「wordpress_」 与 default-constants.php 中设置的 siteurl 的 md5 和连接而形成。这是默认行为,可以从配置文件内部通过先前设置一些常量来覆盖。

认证 cookie 是用户名的连接,认证 cookie 有效的时间戳,以及 HMAC,对于那些拉 TL 的人来说,是一种 key-biased 哈希值。三个变量与管道字符串连接。

以下是 HMAC 的构建方式:

$hash = hash_hmac('md5', $username . '|' . $expiration, wp_hash($username . substr($user->user_pass, 8, 4) . '|' . $expiration, $scheme));

这是安全吗?

根据 this article,这个答案中的大部分信息都来自于它,如果你知道你的独特的短语,并且如果你的密钥是独一无二的,那么他们知道你的独特的短语是 200,000,000,000,000,000,000,000,000,000,000 倍,那么这个星期将会是一个黑社会,要求一个 30 秒的请求。

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。