前端AJAX用户登录会话问题

时间:2018-10-13 作者:Faruk

我遇到了一个问题,ajax登录表单允许我登录。如果我在登录后将重定向设置为主页,它会将我发送到主页,并在顶部为我提供管理栏。但是,如果我尝试转到管理仪表板,它会要求我使用正常的wordpress登录再次登录。

如何解决这个问题?我的代码:

在页面登录中。php模板:

<form id="login" class="ajax-auth" action="login" method="post">
    <p class="status"></p>  
    <?php wp_nonce_field(\'ajax-login-nonce\', \'security\'); ?>  
    <div class="form-group">
        <input id="username" placeholder="<?php _e(\'Username\',\'my-theme\'); ?>" type="text" class="required form-control" name="username">
    </div>
    <div class="form-group">
        <input id="password" placeholder="<?php _e(\'Password\',\'my-theme\'); ?>" type="password" class="required form-control" name="password">
    </div>
    <input class="btn btn-block btn-md btn-info" type="submit" value="<?php _e(\'Login\',\'my-theme\'); ?>">
</form>
在函数中。php:

function ajax_auth_init(){  
  global $theme_options;
  $userProfilePage=$theme_options[\'opt-user-select\'];

  wp_register_script(\'ajax-auth-script\', get_template_directory_uri() . \'/assets/js/ajax-logreg-script.js\', array(\'jquery\') ); 
  wp_enqueue_script(\'ajax-auth-script\');

  wp_localize_script( \'ajax-auth-script\', \'ajax_auth_object\', array( 
    \'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
    \'redirecturl\' => get_page_link($userProfilePage),
    \'loadingmessage\' => __(\'Please wait...\',\'my-theme\')
  ));

  // Enable the user with no privileges to run ajax_login() in AJAX
  add_action( \'wp_ajax_nopriv_ajaxlogin\', \'ajax_login\' );
}
// Execute the action only if the user isn\'t logged in
if (!is_user_logged_in()) {
    add_action(\'init\', \'ajax_auth_init\');
}
function ajax_login(){

  // First check the nonce, if it fails the function will break
  check_ajax_referer( \'ajax-login-nonce\', \'security\' );

    // Nonce is checked, get the POST data and sign user on
    // Call auth_user_login
  auth_user_login($_POST[\'username\'], $_POST[\'password\'], __(\'Login\',\'my-theme\')); 

  die();
}
function auth_user_login($user_login, $password, $login){
  $info = array();
  $info[\'user_login\'] = $user_login;
  $info[\'user_password\'] = $password;
  $info[\'remember\'] = true;

  $user_signon = wp_signon( $info, false );
    if ( is_wp_error($user_signon) ){
    echo json_encode(array(\'loggedin\'=>false, \'message\'=>__(\'Wrong password or username.\',\'my-theme\')));
    } else {
    wp_set_current_user($user_signon->ID); 
        echo json_encode(array(\'loggedin\'=>true, \'message\'=> $login.__(\' successful, redirecting...\',\'my-theme\')));
    }

  die();
}
和ajax logreg脚本。js文件:

jQuery(document).ready(function ($) {

    // Perform AJAX login/register on form submit
    $(\'form#login, form#register\').on(\'submit\', function (e) {
        if (!$(this).valid()) return false;
        $(\'p.status\', this).show().text(ajax_auth_object.loadingmessage);
        action = \'ajaxlogin\';
        username =  $(\'form#login #username\').val();
        password = $(\'form#login #password\').val();
        email = \'\';
        security = $(\'form#login #security\').val(); 
        ctrl = $(this);
        $.ajax({
            type: \'POST\',
            dataType: \'json\',
            url: ajax_auth_object.ajaxurl,
            data: {
                \'action\': action,
                \'username\': username,
                \'password\': password,
                \'email\': email,
                \'security\': security,
            },
            success: function (data) {
                $(\'p.status\', ctrl).text(data.message);
                if (data.loggedin == true)
                  document.location.href = ajax_auth_object.redirecturl;
                else if (ctrl == \'register\')
                  grecaptcha.reset();
            }
        });
        e.preventDefault();
    });

    $(".logreg-page form#login").validate({
        ignore: ".ignore",
       rules: {
         username: {
            required: true,
          },
          password: {
            required: true,
          },
       },
       messages: {
          username: "Please enter an username.",
          password: "Please enter your password.",
        },
    });
});

1 个回复
SO网友:Willi

您没有设置身份验证cookie。的第二个参数wp_signon 函数设置为false。

此函数用于设置身份验证cookie。如果未发送,用户将不会登录。

$user_signon = wp_signon( $info, true );
那会给你你想要的结果

结束

相关推荐

Disallow second login session

我现在正在努力寻找一个解决方案,持续了两个月,但不幸的是,我没有找到任何结果。我有一个基于订阅的网站,可以播放音乐。客户基本上购买的是一个“面板”,他们在那里听音乐without the need to refresh 页面。我需要的是,如果他们的帐户已经在使用中,就不允许他们登录。我发现许多插件会强制用户注销,但只有在刷新页面时才会发生这种情况。例如,如果:A登录,它也可以工作。B使用相同的帐户登录,此时A将自动注销(我猜是使用Ajax)。过去没有人需要这个,这有点奇怪。有人能帮忙吗?提前谢谢。