简体   繁体   English

仅使用用户名登录的用户

[英]Login user with username only

I have 2 scripts running in the same server: proXcore (root directory) and wordpress (sub-directory). 我在同一服务器上运行2个脚本:proXcore(根目录)和wordpress(子目录)。

What I need to accomplish is: when the user logs into proXcore, the user gets automatically logged into wordpress. 我需要完成的是:当用户登录到proXcore时,用户将自动登录到wordpress。 Same thing with the logout. 与注销相同。

When a user signs into proXcore, it creates a $_SESSION['username'] variable with the username, indeed. 用户登录proXcore时,实际上会创建一个带有用户名的$_SESSION['username']变量。 So I am taking advantage of this to try to accomplish what I need. 因此,我利用这一优势来尝试完成我所需要的。 What I have so far in my functions.php of wordpress theme is: 到目前为止,我在wordpress主题的functions.php中拥有的内容是:

function custom_login_wp_and_pxc() {

    // GET USER INFO. WE NEED THE ID!   
    $user = get_user_by( 'login', $_SESSION['username'] );

    // SET THE COOKIE!
    wp_set_auth_cookie( $user->ID, false, '' );

    // USER NEEDS TO RELOAD THE PAGE 2 TIMES
    // WE NEED TO FIND A FIX FOR THIS       
    // WE TRY THIS BUT IT DID NOT REDIRECTED PROPERLY
    //wp_redirect( home_url() );
    //exit;
}

// IF SESSION[USERNAME] EXISTS, THEN IT MEANS IT IS LOGGED IN INTO PROXCORE, 
// THEREFORE, LOG THE USER INTO WORDPRESS
if( isset( $_SESSION['username'] ) and !empty( $_SESSION['username'] ) ){
    // run it before the headers and cookies are sent
    add_action( 'after_setup_theme', 'custom_login_wp_and_pxc' );
}   
// IF SESSION[USERNAME] DOES NOT EXISTS, THEN IT MEANS IT IS NOT LOGGED IN INTO PROXCORE, 
// THEREFORE, LOG OUT THE USER FROM WORDPRESS
else {
    wp_logout();
}

It is working but with a little glitch. 它正在工作,但有一点故障。 If I sign into proxcore, and then I visit the wordpress, I am not signed in at first, I need to visit some other page or post of wordpress. 如果我登录到proxcore,然后访问wordpress,那么我一开始没有登录,我需要访问wordpress的其他页面或帖子。 Just then, I see myself as logged in. 就在那时,我看到自己已登录。

I need the user to be logged in at the first landing visit without needing to click somewhere else for the authentication to take effect. 我需要用户在首次登陆时登录,而无需单击其他位置以使身份验证生效。

I hope I make sense. 我希望我有道理。

Is it the right code I am using here? 我在这里使用的代码正确吗? Is is correct to put this code in the functions.php of the theme? 将此代码放在主题的functions.php中是否正确? Is there another way to accomplish this? 还有另一种方法可以做到这一点吗?

Thank for your help! 谢谢您帮忙!

The after_setup_theme hook runs before WordPress authenticates the user so you may be setting the auth cookie too early. after_setup_theme挂钩会在WordPress对用户进行身份验证之前运行,因此您可能太早设置auth cookie。 What about changing that to: 将其更改为:

add_action( 'init', 'custom_login_wp_and_pxc' );

This will run after WordPress creates the user and tries to authenticate. 这将在WordPress创建用户并尝试进行身份验证后运行。

I made it work! 我成功了!

I am posting the code for if anybody sometime needs it. 我正在发布代码,以备不时之需。

Thank you everybody for your help and suggestions as you enlightening me to find the solution. 感谢您在启发我找到解决方案时的帮助和建议。

function custom_login_wp_and_pxc() {

    if( isset( $_SESSION['username'] ) and !empty( $_SESSION['username'] ) )
        $session_username = $_SESSION['username'];          
    elseif ( isset( $_SESSION['adminusername'] ) and !empty( $_SESSION['adminusername'] ) )
        $session_username = $_SESSION['adminusername'];

    // GET USER INFO. WE NEED THE ID!   
    $user = get_user_by( 'login', $session_username );

    // SET THE COOKIE!
    wp_set_auth_cookie( $user->ID, false, '' );
}

// IF SESSION[USERNAME] EXISTS, THEN IT MEANS IT IS LOGGED IN INTO PROXCORE, 
// THEREFORE, LOG THE USER INTO WORDPRESS
if( ( isset( $_SESSION['username'] ) and !empty( $_SESSION['username'] ) )
    or ( isset( $_SESSION['adminusername'] ) and !empty( $_SESSION['adminusername'] ) ) ){

    if(!is_user_logged_in()){   
        add_action( 'after_setup_theme', 'custom_login_wp_and_pxc' );
        wp_redirect( home_url() );
    }

}   
// IF SESSION[USERNAME] DOES NOT EXISTS, THEN IT MEANS IT IS NOT LOGGED IN INTO PROXCORE, 
// THEREFORE, LOG OUT THE USER FROM WORDPRESS
else {          
    wp_logout();

    if(is_user_logged_in())
        wp_redirect( home_url() );  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM