在登录页面中添加额外的身份验证字段

时间:2012-03-17 作者:nomall

我将增强我的WordPress身份验证页面。我的想法是,只需在登录页面中添加一个额外字段。额外字段要求用户输入个人识别码(4至8位)。

我已在中更改了登录页面布局wp-login.php 并添加了额外的列,用于在MySQL数据库中存储个人标识码。

然而,我不知道哪个部分的编码是用来验证密码的,因为我想参考现有的编码。

我怎样才能有效地做到这一点?

3 个回复
SO网友:Stephen Harris

首先,我会advise against editing the core files 因为下次更新WordPress时它将被覆盖。

此外,您应该更新WordPress,因为它通常包含安全更新。(最近been reported 对使用过时WordPress版本的网站进行了大量攻击)

为了实现您真正想要做的事情,我建议您使用hooks 作为编辑WordPress的最佳方式。

因此,要在登录页面上创建额外字段,可以使用login_form 行动挂钩:

add_action(\'login_form\',\'my_added_login_field\');
function my_added_login_field(){
    //Output your HTML
?>
    <p>
        <label for="my_extra_field">My extra field<br>
        <input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
    </p>
<?php
}
接下来,我们需要验证他们在字段中输入的内容是否与您存储的内容相匹配。在下面的代码中,我假设您已将标识码存储为带有元键的用户元值my_ident_code. You should do this rather than create your own column!. 有关详细信息,请参阅Codex页面

  • add_user_meta 功能update_user_meta 功能get_user_meta 功能验证用户可以使用authenticate 滤器这将传递输入的用户名和密码。如果识别码正确,返回null 允许WordPress验证密码和用户名。如果不正确,请删除WordPress的身份验证并返回错误。这将强制用户返回到登录页面,在那里他们将看到显示的错误。

    add_filter( \'authenticate\', \'my_custom_authenticate\', 10, 3 );
    function my_custom_authenticate( $user, $username, $password ){
        //Get POSTED value
        $my_value = $_POST[\'my_extra_field_name\'];
    
        //Get user object
        $user = get_user_by(\'login\', $username );
    
        //Get stored value
            $stored_value = get_user_meta($user->ID, \'my_ident_code\', true);
    
        if(!$user || empty($my_value) || $my_value !=$stored_value){
            //User note found, or no value entered or doesn\'t match stored value - don\'t proceed.
                remove_action(\'authenticate\', \'wp_authenticate_username_password\', 20);
                remove_action(\'authenticate\', \'wp_authenticate_email_password\', 20); 
    
            //Create an error to return to user
                return new WP_Error( \'denied\', __("<strong>ERROR</strong>: You\'re unique identifier was invalid.") );
        }
    
        //Make sure you return null 
        return null;
    }
    

SO网友:Manimaran

此答案仅供参考。

您不应该编辑核心文件WordPress文件,否则更新后您将丢失所做的修改。

下面是WordPress如何处理其登录密码验证过程,如/wp-includes/class-phpass.php 文件:

function CheckPassword($password, $stored_hash)
{
    $hash = $this->crypt_private($password, $stored_hash);
    if ($hash[0] == \'*\')
        $hash = crypt($password, $stored_hash);

    return $hash == $stored_hash;
}

SO网友:itsdanprice

如果编辑所有这些代码似乎有点令人望而生畏,那么有一个很好的插件:

Cimy Extra Fields

或者您可以考虑使用Theme My login 它可以做到这一点,但需要一些编码。它还添加了相当多的额外功能。您可以愉快地将这两个插件一起使用。

结束

相关推荐

Changing Login Logo

在我的函数中修改代码后。php文件在我的登录页面上显示不同的徽标,我得到的是一个空白方块。我添加了一个自定义项。png图像(326x67)到我的(子)主题的图像文件夹,以及我添加到的代码functions.php 具体如下:function custom_loginlogo() { echo \'<style type=\"text/css\"> h1 a {background-image: url(\'.get_bloginfo(\'template_dir