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

时间:2016-06-28 14:03:51

标签: wordpress curl cookies login single-sign-on

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

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

现在我想要的是,如果我登录我的第一个网站并在同一个浏览器中转到第二个网站的链接,那么我就能够以登录用户的身份看到网页内容。

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

尝试通过两种方法实现这一目标,但仍然无法通过其中任何方法实现这一目标:

方法1 :将表添加到第二个网站并保存用户电子邮件和身份验证密钥。使用curl获取详细信息然后登录。此方法如下所述:http://carlofontanos.com/auto-login-to-wordpress-from-another-website

但正如我之前提到的那样,我的网站都是私有内容,因此在这种情况下我无法使用curl获取详细信息。我的curl代码如下:

$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没有显示在我的第二个网站上。 我不太熟悉cookies,设置auth cookies等。

请提出解决方案,我该如何实现这一目标。

2 个答案:

答案 0 :(得分:0)

您可以使用&#34;单点登录&#34;

指令 -

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

检查用户是否已登录:     $ signon = new SingleSignon();     $ userId = $ signon-&gt; checkCookie();     如果($用户id){     //用户已登录,这是他们的ID     }      其他{     //用户未登录或已被解除     }

如果用户未登录,请使用我们的api登录,如果api呼叫成功,请使用以下代码。

将用户设置为已登录:     $ signon = new SingleSignon();     $ signon-&GT; setCookie方法($用户id);

注意:您需要使用ssl来读取cookie。

答案 1 :(得分:0)

我已经完成了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-website-another-wordpress

相关问题