Hi,

does anyone know, howto unset user data(meant some variables in variable "data"). I try this but it doesnt work...


function unsetUserData() {
        global $user;
        $user_data = unserialize($user->data);
        unset($user_data['variable_which_i_created_before']);
        user_save( $user , $user_data);   
}

Comments

j_ten_man’s picture

I think you need to do something like this:

function unsetUserData() {
        global $user;
        $user_data = array();
        unset($user->variable_which_i_created_before);
        user_save( $user , $user_data);   
}

You may take a look at user_save but I'm pretty sure it takes all of the unsaved key values from the user object and saves them in the data array.

vembloud’s picture

I tried it but it doesnt work. Variable is stil set...

jaypan’s picture

Where are you calling this function?

Contact me to contract me for D7 -> D10/11 migrations.

vembloud’s picture

Function is called in Menu callback function.


function module_menu() { 
  .
  .
  .
  $items['module/user/logout'] = array(
    'page callback' => 'module_logout',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  .
  .
  .
}


function module_logout() {
  $userHandler = new IsUsers();
  $userHandler->logoutUser();
  drupal_goto('module/user/login');    
}

Class IsUsers is for another system users (lets call IS).
I save infomation about IS user login into drupal user variable.

    public function login($vals) {
        global $user;
        $user_data = array(
            'Login'  => $vals['IDLogin'],     
            'Role'   => $vals['IDRole'],   
            'Unit'   => $vals['IDUnit'],    
            'expire'    => time() + 30 * 60     
        );
        $updated_user = user_save( user_load($user->uid) , $user_data);
    }

And when IS user logout I want to clear drupal user data related to IS login (Login, Role, Unit, epire).

    public function logoutUser() {
        global $user;
        $user_data = array();
        unset($user->Login, $user->Role, $user->Unit, $user->expire);
        user_save( $user , $user_data);  
    }
rouhipour.marjan’s picture

I know 'account' is a default value for user_save category argument. But pass it manually to the user_save.
Without it, it didn't work for me when I wanted to update user.

Best,
Marjan

vembloud’s picture

Sorry, I dont understand, can you show me an example?

j_ten_man’s picture

You forced me to open up my project and look how we actually do it. I just remembered wrong :)

function unsetUserData() {
        global $user;
        $user_data = array('variable_which_i_created_before' => NULL);
        user_save( $user , $user_data);   
}

This is definitely working for us. I hope it works for you.

vembloud’s picture

Great, it works fine ;) Thanks a lot...