Wordpress登录表单验证

时间:2018-02-18 08:09:39

标签: wordpress validation login

我已经通过验证创建了自定义wordpress登录但是一旦提交电子邮件和密码就会出现问题,即使成功或错误也没有验证我尝试构建自定义用户身份验证我创建了注册和重置密码休息是开发中的登录和个人资料页面,所以我创建了所有需要的重定向,以允许我的自定义页面取代wordpress默认页面,所以我需要知道我的代码有什么问题以及如何在登录后重定向这里的自定义页面是我的代码

<?php
    global $wpdb;

    $error = '';
    $success = '';

            // check if we're in login form
    if( isset( $_POST['action'] ) && 'login' == $_POST['action'] ) 
    {
        $email = trim($_POST['log']);
        $password = trim($_POST['pwd']);

        if( empty( $email ) || empty( $password ) ) {
            $error = 'Enter a username or e-mail address..';
        } else if( ! password_exists( $password ) ) {
            $error = 'password is incorrect.';
        } else if( ! is_email( $email )) {
            $error = 'Invalid username or e-mail address.';
        } else if( ! email_exists( $email ) ) {
            $error = 'There is no user registered with that email address.';
        } else {
            if( email_exists( $email ) ) {
                $success = 'successfully loged in.';

        } else {
            $error = 'Oops something went wrong while loging in to your account.';
        }

        }

        if( ! empty( $error ) )
            echo 'error text'; 

        if( ! empty( $success ) )
            echo 'error text'; 
    }
<!-- Show logged out message if user just logged out -->
<?php if ( $attributes['logged_out'] ) : ?>
<p class="login-info">
    <?php _e( 'You have signed out. Would you like to sign in again?', 'personalize-login' ); ?>
</p>
<?php endif; ?>

<?php if ( $attributes['registered'] ) : ?>
<p class="login-info">
    <?php
            printf(
                __( 'You have successfully registered to <strong>%s</strong>. We have emailed your password to the email address you entered.', 'personalize-login' ),
                get_bloginfo( 'name' )
            );
        ?>
</p>
<?php endif; ?>

<?php if ( $attributes['lost_password_sent'] ) : ?>
<p class="login-info">
    <?php _e( 'Check your email for a link to reset your password.', 'personalize-login' ); ?>
</p>
<?php endif; ?>

<?php if ( $attributes['password_updated'] ) : ?>
<p class="login-info">
    <?php _e( 'Your password has been changed. You can sign in now.', 'personalize-login' ); ?>
</p>
<?php endif; ?>


<!-- START Login Form -->
<form id="form-login" class="p-t-15" action="<?php echo wp_login_url(); ?>">
    <!-- START Form Control-->
    <div class="form-group form-group-default">
        <label>Login</label>
        <div class="controls">
            <?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
            <input type="text" name="log" id="user_login" placeholder="User Name" class="form-control" required>
        </div>
    </div>
    <!-- END Form Control-->
    <!-- START Form Control-->
    <div class="form-group form-group-default">
        <label>Password</label>
        <div class="controls">
            <?php $user_pass = isset( $_POST['user_pass'] ) ? $_POST['user_pass'] : ''; ?>
            <input type="password" class="form-control" name="pwd" id="user_pass" placeholder="Credentials" required>
        </div>
    </div>
    <!-- START Form Control-->
    <div class="row">
        <div class="col-md-6 no-padding sm-p-l-10">
            <div class="checkbox ">
                <input type="checkbox" value="1" id="checkbox1">
                <label for="checkbox1">Keep Me Signed in</label>
            </div>
        </div>
        <div class="col-md-6 d-flex align-items-center justify-content-end">
            <a href="#" class="text-info small">Help? Contact Support</a>
        </div>
    </div>
    <!-- END Form Control-->
    <button class="btn btn-primary btn-cons m-t-10" type="submit">Sign in</button>
</form>
<!--END Login Form-->

1 个答案:

答案 0 :(得分:1)

表单元素由name传递,而不是id传递,即在您的情况下,您需要获取$_GET['log']用户名,而不是$_GET['user_login']

但是:此代码存在许多问题。您不应该通过GET传递私人信息,不应该告诉用户登录错误是否涉及用户名或密码等。如果您在考虑最佳实践时重写此代码,将更容易找到功能流程的问题。