wp_set_current_user (remote logging) doesnt work on server

I am figting with one interesting problem. I am logging in user with authorization on remote server. The problem is that it is working fine on a localhost but not on a server.

Here is code I am using:

Read More
function userSignIn() {
$token = $_GET['key'];
$userOp = getUserFromOP($token);
if ($userOp != null && is_array($userOp)) {
    $userobj = new WP_User();
    $myuser = $userobj->get_data_by('email', $userOp['Email']);
    $myuser = new WP_User($myuser->ID);
    if ($myuser->ID == 0) {
        $userdata = array(
            'user_email' => $userOp['Email'],
            'user_login' => $userOp['Email'],
            'display_name' => $userOp['Name'],
            'role' => 'contributor'
        );
        $new_user_id = wp_insert_user($userdata);
        update_user_meta($new_user_id, 'zip_code', $userOp['ZIP']);
        update_user_meta($new_user_id, 'phone', $userOp['Phone']);
        update_user_meta($new_user_id, 'user_field', $userOp['Field']);
        update_user_meta($new_user_id, 'user_obor_zamestnavatele', $userOp['OborZamestnavatele']);
        update_user_meta($new_user_id, 'user_type', $userOp['UserType']);
        $myuser = new WP_User($new_user_id);
        wp_set_current_user($myuser->ID);
        wp_set_auth_cookie($myuser->ID, true);
    } else {
        var_dump($myuser);                       //no problem with user
        wp_set_current_user($myuser->ID);        //problem is here
        wp_set_auth_cookie($myuser->ID, true);   //and here
    }
    return true;
} else {
    return false;
}

}

I have no problem with getting and creating user. I have a user in DB and when I dump it I can see it but I am not able to set it with those two functions.

dumped object:

object(WP_User)#2048 (7) { 
["data"]=> object(stdClass)
    #2047 (10) { 
        ["ID"]=> string(1) "7" 
        ["user_login"]=> string(27) "email@email.com" 
        ["user_pass"]=> string(34) "$P$BXIG9QYI4ij6ZmWm73T8TerQ5V2bGQ1" 
        ["user_nicename"]=> string(26) "emailemail-com" 
        ["user_email"]=> string(27) "email@email.com" 
        ["user_url"]=> string(0) "" 
        ["user_registered"]=> string(19) "2013-09-03 12:39:47" 
        ["user_activation_key"]=> string(0) "" 
        ["user_status"]=> string(1) "0" 
        ["display_name"]=> string(13) "name surname" } 
    ["ID"]=> int(7) 
    ["caps"]=> array(1) { 
        ["contributor"]=> bool(true) } 
    ["cap_key"]=> string(24) "wp_nadaceop_capabilities" 
    ["roles"]=> array(1) { 
            [0]=> string(11) "contributor" } 
    ["allcaps"]=> array(6) { 
        ["edit_posts"]=> bool(true) 
        ["read"]=> bool(true) 
        ["level_1"]=> bool(true) 
        ["level_0"]=> bool(true) 
        ["delete_posts"]=> bool(true) 
        ["contributor"]=> bool(true) } 
    ["filter"]=> NULL } 

Can anybody help me?

Thank you really a lot.

Michal Majernik.

Related posts