自定义哈希方法后阻止登录Cookie

时间:2021-02-24 作者:Jan

尝试登录时出现以下错误:

错误:由于意外输出,Cookie被阻止
enter image description here

我定制了wordpress的核心功能wp_hash_password()wp_check_password() 使用SHA1算法进行用户身份验证,而不是MD5。我在插件中修改了它们。php文件(不是最佳实践,但适合测试)。函数现在如下所示:

function wp_hash_password( $password ) {
            global $wp_hasher;

            if ( empty( $wp_hasher ) ) {
                    $wp_hasher = sha1( $password );
            }

            return $wp_hasher->HashPassword( trim( $password ) );;
    }

function wp_check_password( $password, $hash, $user_id = \'\' ) {
            global $wp_hasher;
            // If the hash is still md5...
            if ( strlen( $hash ) == 40 ) {
                    echo \'<h2>\'.$hash.\'</h2>\';
                    $check = hash_equals( $hash, sha1( $password ) );
                    if ( $check && $user_id ) {
                            // Rehash using new hash.
                            wp_set_password( $password, $user_id );
                            $hash = wp_hash_password( $password );
                    }

                    /**
                     * Filters whether the plaintext password matches the encrypted password.
                     *
                     * @since 2.5.0
                     *
                     * @param bool       $check    Whether the passwords match.
                     * @param string     $password The plaintext password.
                     * @param string     $hash     The hashed password.
                     * @param string|int $user_id  User ID. Can be empty.
                     */
                    return apply_filters( \'check_password\', $check, $password, $hash, $user_id );
            }

            // If the stored hash is longer than an MD5,
            // presume the new style phpass portable hash.
            if ( empty( $wp_hasher ) ) {
                    require_once ABSPATH . WPINC . \'/class-phpass.php\';
                    // By default, use the portable hash from phpass.
                    $wp_hasher = new PasswordHash( 8, true );
            }

            $check = $wp_hasher->CheckPassword( $password, $hash );

            /** This filter is documented in wp-includes/pluggable.php */
            return apply_filters( \'check_password\', $check, $password, $hash, $user_id );
    }
从密码到SHA1的转换与密码检查一样完美。我认为负责设置cookie的函数仍在使用MD5。但我无法验证,也找不到函数
有人对此有想法吗

提前感谢

1 个回复
最合适的回答,由SO网友:vancoder 整理而成

在设置cookie之前,正在输出此行(至少):

echo \'<h2>\'.$hash.\'</h2>\';
将输出发送到屏幕(甚至空行)将阻止设置Cookie。

PHP docs:

与其他标头一样,cookie必须在脚本的任何输出之前发送(这是协议限制)。这要求您在任何输出之前对该函数进行调用,包括<html><head> 标记以及任何空格。

相关推荐

在Gutenberg/JS中使用Dispatch(‘core/EDITOR’).editPost()调用将多个元值设置为数组

我试图通过Gutenberg组件将图像的URL和ID存储在帖子的元字段中,但我很难弄清楚如何将这些多个值存储在一个数组中。我可以毫无问题地将单个值存储为元值。我甚至可以毫无问题地将单个值存储在数组的第一个位置。但我似乎无法找到合适的语法来利用Wordpress将数组存储为元值的能力。总体目标是覆盖在社交媒体上分享帖子时使用的图像。问题是,我需要将URL和ID都传输到PHP挂钩,以便它能够与插件(SEO框架)正常工作。不幸的是,我不能只存储ID,因为我需要URL来在Gutenberg组件中呈现图像。完整组件