两个完全独立的WordPress网站的链接登录



我有两个WordPress网站在服务器的子域上运行,例如http://first.mywebsites.net和http://second.mywebsites.net.net

他们俩都像私人网站一样,如果我登录到网站,我可以看到页面的内容,否则重定向到登录页面。

现在我想要的是,如果我登录第一个网站并转到同一浏览器的第二个网站的链接,那么我可以将页面的内容视为登录的用户。

这必须发生仅在第二个网站数据库中具有相同用户(用相同邮件ID注册的用户注册的用户)中登录的第一个网站的用户。与我的网站一样,大多数用户在两个网站中都有相同的邮件ID注册。

试图通过两种方法实现这一目标,但仍然无法获得任何方法:

方法1 :将表添加到第二个网站并保存用户电子邮件和auth键。使用卷曲来获取细节然后登录。此方法如下所述:http://carlofontanos.com/auto-login-to-wordpress-from-another-website

,但是正如我之前提到的那样,我的情况下两个网站都有私人内容,因此在这种情况下,我无法使用卷发来获取细节。我的卷发代码就像:

$api_url = "http://second.mywebsites.net/autologin-api/";
    // If you are using WordPress on website A, you can do the following to get the currently logged in user:
    global $current_user;
    $user_email = $current_user->user_email;
    // Set the parameters
    $params = array(
        'action'            => 'get_login_key', // The name of the action on Website B
        'key'               => '54321', // The key that was set on Website B for authentication purposes.
        'user_email'       => $user_email // Pass the user_email of the currently logged in user in Website A
    );
// Send the data using cURL
    $ch = curl_init($api_url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $gbi_response = curl_exec($ch);
    curl_close($ch);
    // Parse the response
    parse_str($gbi_response);
    print_r($gbi_response);

在这种情况下,我没有得到响应,我的页面将我重定向到第二个网站的登录页面。

方法2 :尝试使用cookie来完成此操作,因为我想在同一浏览器中登录第二个网站。我在我的第一个网站上添加了一个新cookie,例如:

global $current_user;
$user_email = $current_user->user_email;
if($user_email != ''){
    $_COOKIE['current_user_mail_id'] = $user_email;
}
echo "<pre>";
print_r($_COOKIE);
echo "</pre>";

和添加的cookie与其他cookie一起显示。但是,当我在我的第二个网站上在同一浏览器上检查此内容时,例如:

echo "<pre>";
print_r($_COOKIE);
echo "</pre>";

我在第一个网站上添加的cookie没有在我的第二个网站上显示。我对cookie,设置cookie等不太熟悉

请建议解决方案,我该如何实现。

您可以使用"单签名"来完成。

指令 -

为了简化系统之间的传递用户,我创建了这个PHP类欢迎您使用。

检查用户是否已签名: $ signon = new Singlesignon(); $ userId = $ signon-> chchnecookie(); 如果($ userId){ //用户已登录,这是他们的ID } 别的{ //用户未登录或被登录 }

如果未登录用户,请使用我们的API登录,然后如果API调用成功,请使用以下代码。

将用户设置为登录: $ signon = new Singlesignon(); $ signon-> setCookie($ userId);

注意:您需要使用SSL才能阅读cookie。

我已经完成了Gaurav提供的解决方案,并找到了一个想法,可以使WordPress网站成为可能。

位置下面提到的代码位于您要将链接放入第二个网站的地方:

<?php global $current_user;
                                $user_email = $current_user->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 '<div class="dtNode"><a href="http://second.mywebsites.net/sso.php?key='.$email_encoded.'&detail='.$user_login_encoded.'" target="_blank">Link to second website</a></div>';
                        }?>

现在准备一个sso.php文件,并将其放在您要自动登录的第二个站点的根安装中。现在将以下代码放在那里:

<?php
require_once( 'wp-load.php' ); //put correct absolute path for this file

global $wpdb;
if(isset($_GET['key']) && !empty($_GET['key'])){
    $email_decoded = base64_decode(strtr($_GET['key'], '-_', '+/'));
    $username_decoded = base64_decode(strtr($_GET['detail'], '-_', '+/'));
    $received_email = sanitize_text_field($email_decoded);
    $received_username = sanitize_text_field($username_decoded);

    if( email_exists( $received_email )) {
            //get the user id for the user record exists for received email from database 
            $user_id = $wpdb->get_var($wpdb->prepare("SELECT * FROM wp_users WHERE user_email = %s", $received_email ) );
            wp_set_auth_cookie( $user_id); //login the user
            wp_redirect( 'http://second.mywebsites.net');
    }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 willl 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 willl 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( 'http://second.mywebsites.net');
            }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;
} ?>

上面的代码对我有用,您可以根据需要修改代码。有关更明确的解释,您可以在此处查看:http://www.wptricks24.com/auto-login-one-no-website-another-wordpress

最新更新