automatic login to wordpress dashboard from another site

前端 未结 2 1088
你的背包
你的背包 2020-12-15 01:53

I want to log in automatically to WP admin/dashboard from another site without going thru the login process.. I\'ve tried the following but with no luck:

<         


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 02:51

    Try This complete code it working 100% First Website : http://firstwebsite.com and Second Website : http://secondwebsite.com

    Now, first of all create a link on your first website, on which we want to click to go to our second website as a logged in user. So, in your first website create a link at your desired place as mentioned below :

    user_email;
    $user_login = $current_user->user_login;
    if($user_email != ''){
    
    $email_encoded = rtrim(strtr(base64_encode($user_email), '+/', '-_'), '='); 
    $user_login_encoded = rtrim(strtr(base64_encode($user_login), '+/', '-_'), '='); 
    echo 'Link to 
    second website';
    }?> 
    

    Now, open our second website and create a new php file and name it as “sso.php”. Place this file at your root installation and just copy paste the below mentioned code in this file :

    get_var($wpdb->prepare("SELECT * FROM ".$wpdb->users." WHERE user_email = %s", $received_email ) );
    
            wp_set_auth_cookie( $user_id); //login the previously exist user
    
            wp_redirect(site_url()); // put the url where you want to redirect user after logged in
    
    }else {
    
            //register those user whose mail id does not exists in database 
    
            if(username_exists( $received_username )){
    
                //if username coming from first site exists in our database for any other user,
                //then the email id will be set as username
                $userdata = array(
                'user_login'  =>  $received_email,
                'user_email'  =>  $received_email, 
                'user_pass'   =>  $received_username,   // password will be username always
                'first_name'  =>  $received_username,  // first name will be username
                'role'        =>  'subscriber'     //register the user with subscriber role only
            );
    
            }else {
    
                $userdata = array(
                'user_login'  =>  $received_username,
                'user_email'  =>  $received_email, 
                'user_pass'   =>  $received_username,   // password will be username always
                'first_name'  =>  $received_username,  // first name will be username
                'role'        =>  'subscriber'     //register the user with subscriber role only
            );
    
            }
    
    
            $user_id = wp_insert_user( $userdata ) ; // adding user to the database
    
            //On success
            if ( ! is_wp_error( $user_id ) ) {
                 
                wp_set_auth_cookie( $user_id); //login that newly created user
                wp_redirect(site_url()); // put the url where you want to redirect user after logged in
    
            }else{
    
                echo "There may be a mismatch of email/username with the existing record.
                      Check the users with your current email/username or try with any other account.";die;
            }
    
    
    }
    
     die;
    
     } ?>
    

提交回复
热议问题