Friday, 13 January 2012

the first time login in drupal


it certainly look pretty simple to detect the first time login event using hook_user to check for "login" operation and check the users object "$user->login" value.


  function mycustom_user($op,&$edit,&$account,$category = NULL){  
 if($op == 'login'){  
 if($account->login == 0){ // check the last login  
 //do stuff here  

this work just fine if you want to impletement a redirection once the user login for the first time.

but what if you have to check users first time login on search page or user profile that particular condition the above code does not work. because the
the value of user->login will be updated as soon as user login.

there is a simple module which manage the user Statistics called user_stats 
it manage the user login count which comes in handy here.

here is the code to get the login count from user_stats

  global $user  
 if(user_stats_get_stats('login_count',$user->uid) == 1){ //check that its user first time login  
 drupal_set_message("some message");  
 //do the stuff  

well you can use this module in many way for getting reg date much more..

well this come in very handy to solve my problem.

1 comment: