不要在错误的代码后提交表格

道伊

我正在研究一个WordPress插件,它将auth代码添加到登录表单。

这是检查身份验证代码是否有效的过程:

add_action( 'wp_authenticate', 'authcode_check', 5 );
function authcode_check($username) {
    $options = get_option( 'authcode_settings' );
    if(!empty($options['code'])) {
        global $wpdb;

        if ( !username_exists( $username ) ) {
            return;
        }

        $set_code = $options['code'];
        $submit_code = $_POST['auth_key'];

        if(empty($submit_code)) {
            add_filter( 'login_errors', function( $error ) {$error = '<strong>ERROR</strong>: Authentication code cannot be empty.';return $error;} );
            return;
        } elseif ( ! ( $set_code == $submit_code ) ) {
            add_filter( 'login_errors', function( $error ) {$error = '<strong>ERROR</strong>: Authentication code is invalid.';return $error;} );
            return;
        }
    }
}

问题是; 当用户正确输入其WordPress名称和密码(而不是验证码)时,表单仍会提交并登录用户。

我试过了,return false但是没有用。

当用户输入错误的身份验证代码时,有什么方法可以防止表单登录用户?

比尔吉雷

工作实例

与@ J.Doe聊天后更新

我们可以将其login_form钩住,以显示认证代码的输入

/**
 * 'Authentication Code' Input
 */
add_action( 'login_form', function() 
{
    // Fetch the stored code
    $options = get_option( 'authcode_settings' );

    // Display code input
    if( isset( $options['code'] ) ) 
        printf(
           '<p class="login-authenticate">
            <label for="auth_key">%s</label>
            <input type="text" name="so38551606_auth_key" id="so38551606_auth_key" 
                   class="input" value="" size="20" autocomplete="off" />
            </p>',
            esc_html__( 'Authentication Code', 'mydomain' )
        );
} );

您可以authenticatewp_authenticate()函数连接到过滤器以进行验证:

/**
 * Validate 'Authentication Code' Input
 */
add_filter( 'authenticate', function( $user )
{
    // Fetch stored code value
    $options = get_option( 'authcode_settings' );

    // Nothing to do if there's no stored code value
    if( ! isset( $options['code'] ) )
        return $user;

    // Fetch the user's code input
    $submit_code = isset( $_POST['so38551606_auth_key'] ) 
        ? $_POST['so38551606_auth_key'] 
        : null;

    // Validation's logic       
    $is_valid_auth_code = ! is_null( $submit_code ) 
        && ( $options['code'] === $submit_code );

    // Valid auth code  
    if( $is_valid_auth_code )
        return $user;

    // Add an unvalid auth code error
    if( is_wp_error( $user ) )
        $user->add( 
            'invalid_auth_code', 
            sprintf(
            '<strong>%s</strong>: %s',
            esc_html__( 'ERROR', 'mydomain' ),
            esc_html__( 'Authentication code is invalid.', 'mydomain' )
        )
    ); 
    // Create a new auth code error
    else
        $user = new WP_Error(
            'invalid_auth_code', 
            sprintf(
                '<strong>%s</strong>: %s',
                esc_html__( 'ERROR', 'mydomain' ),
                esc_html__( 'Authentication code is invalid.', 'mydomain' )
            )
        ); 

    return $user;
}, 100 );

在这里,我们使用优先级为,100因为我们要在默认的回调之后运行它:

add_filter( 'authenticate', 'wp_authenticate_username_password',  20, 3 );
add_filter( 'authenticate', 'wp_authenticate_email_password',     20, 3 );
add_filter( 'authenticate', 'wp_authenticate_spam_check',         99    );

我们为POST变量加上前缀,so38551606_以避免可能的名称冲突。

输出示例:

auth输入密钥验证示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

不要在错误的代码后提交表格

来自分类Dev

提交表格后如何清除验证错误

来自分类Dev

点击后提交表格

来自分类Dev

提交后重置表格

来自分类Dev

将提交推送到github后,代码更改显示错误

来自分类Dev

设计登录后提交表格

来自分类Dev

提交后的JSF重置表格

来自分类Dev

提交表格后发送sendEmail

来自分类Dev

PHP的实时表格提交后

来自分类Dev

提交表格后更新CSRF

来自分类Dev

解除绑定后提交表格

来自分类Dev

提交后如何清除表格

来自分类Dev

提交后重置我的表格

来自分类Dev

提交后隐藏表格行

来自分类Dev

提交后再次显示表格

来自分类Dev

提交表格后关闭窗口

来自分类Dev

永远不要在子模块中提交我的更改

来自分类Dev

git,合并后不要轻易提交

来自分类Dev

不要在Aspose PDF中的表格上分页吗?

来自分类Dev

转载提交了错误的表格

来自分类Dev

提交时的联系表格错误

来自分类Dev

提交按钮提交错误的表格

来自分类Dev

提交特定文件时不要执行 SVN“提交后挂钩”

来自分类Dev

提交联系表格后如何在导轨中使用锚查看错误消息

来自分类Dev

不要在foreach引导程序内循环代码

来自分类Dev

Phoenix IEx.pry不要在代码中放置断点

来自分类Dev

不要在Markdown代码块中转义标签

来自分类Dev

不要在回栈后重新加载Fragment的数据

来自分类Dev

取消要提交的表格,不要更改实体的数据