1

I have seen tons of posts on how to do this but nothing i have tried seems to work on my situation. I am going crazy with anticipation to get it working.

I am trying to log into a wordpress site A from a different url and so when a user logs into wordpress site B they get auto logged into wordpress site A. NOTE: the sites are on the same server just different urls.

I have tried CURL and have gotten everything working properly (sending and receiving the data) however it doesn't seem like the cookies are being stored properly and well never log me onto the site. I am doing security on the password i just got rid of it to post it here

So in more detail here is the code to which i am sending the CURL from (Site B)

add_filter('wp_authenticate', 'send_login', 100, 3);
function send_login($username, $password) {
    // this filter is called on the log in page
    // make sure we have a username before we move forward
    if (!empty($username)) {

    //send login information to other sites
    $fields = array( 'username' => $username , 'password' =>   $password );
    echo "<br /> pwd: ". $fields['password'];
    $response = do_post_request('http://www.wordpressSiteA.com/wp-content/plugins/login-api/login.php' , $fields );
    echo $response;
    exit;  // i have this for testing purposes so i dont have to keep logging in and out to test
    return $user;
    }
    return $user;
}



 function send_data_to_sister_sites($url , $fields ) {
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
rtrim($fields_string,'&');
$cookie = "cookie.txt";
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURL_COOKIEFILE, '');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);




//close connection
curl_close($ch);

return $result;
}

And then here is on Site A the login.php file i am sending the CURL too to login the user

require_once("../../../wp-blog-header.php");
//check security of this request and check fields are sent properly
if (isset ($_REQUEST['username'] ) && isset($_REQUEST['password']) ) {
    $username = $_REQUEST['username'];
    $userinfo = get_user_by('login', $username);
    if ($userinfo) {


        //parse data and decrypt fields
        $password =  $_REQUEST['password'];

        $creds = array();
        $creds['user_login'] = $username;
        $creds['user_password'] = $password;
        $creds['remember'] = false;
        //log in user

        wp_signon($creds, true);
        wp_set_auth_cookie( $userinfo->ID );
        wp_set_current_user($userinfo->ID);

    //  global $current_user;
        //get_currentuserinfo();
    //  echo 'name: ' . $current_user->user_login . '<br />';
        if ( is_wp_error($user) )
           echo $user->get_error_message();
        echo "Success";
    } else {
        //no user found exit false
        echo "no user found";
    }
} else { echo "no paramters exist"; }

I have ran this script regularly calling it from Site A like this and it works fine, user gets logged in.

www.wordpressSiteA.com/wp-content/plugins/login-api/login.php?username=username&password=password

However on the CURL request nothing seems to save. Can anyone think of something to help me in the right direction. I am sooo close!

Thanks!

Pengume
  • 550
  • 11
  • 27
  • Do you have control over both domains? – Bailey Parker Jul 14 '12 at 07:44
  • yes I do. they are both my sites. – Pengume Jul 14 '12 at 08:06
  • I've tired a similar approach as this answer http://stackoverflow.com/a/1486474/839628 but i think i am missing a piece of the puzzle for this to work. i dont see why it shouldnt since the script to log in is ran on the site i want to log into.. – Pengume Jul 14 '12 at 08:10

2 Answers2

0

word press like other web apps uses session for login. and there is a problem in sessions when working with different domains.they are not sent to any other site even any other sub domain let alone another domain.so try a way to send your sessions!!!

Abadis
  • 2,671
  • 5
  • 28
  • 42
  • Yeah thanks. i figured it was somethign to do with cross domains or something . I have been trying a lot of things , Im gonna keep trying. Thanks – Pengume Jul 14 '12 at 05:20
0

Since they are on the same server, it is possible to share sessions between both sites but not by reading cookie. you could for instance, store session id, ip and last visit time in database on both sites, and read that database table in both, if same ip and visited within short span and there was no logout, then create new session based on that session id and log the user in. This might open door for some security issues, but it can be done.

See also this: Session Share Across Multiple Domains On Same Server

Community
  • 1
  • 1
Dreaded semicolon
  • 2,274
  • 1
  • 28
  • 43
  • I guess the most confusing part to me is why this wont work when the login.php script is being executed on the actual site i want to log into with. i dont see why that cant work... Do i need to manipulate the headers in order for this to work properly? i may just end up writing to the database and than check that. – Pengume Jul 14 '12 at 07:29
  • because when you create a session , it needs to set a cookie to store the session id. and since you are connecting using domain A, and posting to domain B. Domain B will tries to set cookie, but the cookie won't set on user machine, but on your server that did the curl which will ignore that cookie anyway. the end user won't receive the cookie, when he/she visits domain B, there is no cookie to tell the site the session id. – Dreaded semicolon Jul 14 '12 at 13:47