Insert User log entry when user login

Hey,

I have a table, name User_log, if a user login then I want to insert his information to that table. here is my userIdentity

public function authenticate()


{


	$user=User::model()->with('role')->find('LOWER(loginName)=?',array(strtolower($this->username)));


	





		


	if($user===null)


	{


		$this->errorCode=self::ERROR_USERNAME_INVALID;


	}


	else if(md5($this->password)!==$user->password)


	{


		$this->errorCode=self::ERROR_PASSWORD_INVALID;


	}


	else


	{


		$this->_id=$user->id;


		$this->username=$user->loginName;


		$this->_role=$user->role->name;


		$this->errorCode=self::ERROR_NONE;


		User::model()->updateByPk($user->id,array('lastLogin'=> date("Y-m-d H:i:s")));


		


	}


	return !$this->errorCode;


}

here is my login controller

public function actionLogin()

{


	$form=new LoginForm;


	


	if(isset($_POST['LoginForm']))


	{


		$form->attributes=$_POST['LoginForm'];


		


		if($form->validate())


			


			$this->redirect(Yii::app()->user->returnUrl);


	}





	


	$this->layout='initial';


	$this->render('login', array('form'=>$form));


}

I want to insert user id, time and ip to User_log table

Can anyone tell me how to save the user info to the User_log table?

maybe





	public function authenticate()

	{

		$user=User::model()->with('role')->find('LOWER(loginName)=?',array(strtolower($this->username)));

		

	

			

		if($user===null)

		{

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		}

		else if(md5($this->password)!==$user->password)

		{

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		}

		else

		{

			$this->_id=$user->id;

			$this->username=$user->loginName;

			$this->_role=$user->role->name;

			$this->errorCode=self::ERROR_NONE;


			User::model()->updateByPk($user->id,array('lastLogin'=> date("Y-m-d H:i:s")));


// new code

$user_log= new User_log();

$user_log->id=$user->id;

$user_log->time=date("Y-m-d H:i:s");

$user_log->ip=$_SERVER['REMOTE_ADDR'];

$user_log->save();




		}

		return !$this->errorCode;

	}



[edit]

i found this code, maybe help you




function getRealIpAddr()

{

    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet

    {

      $ip=$_SERVER['HTTP_CLIENT_IP'];

    }

    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy

    {

      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];

    }

    else

    {

      $ip=$_SERVER['REMOTE_ADDR'];

    }

    return $ip;

}



Data not inserting User_log table.

this work?




User::model()->updateByPk($user->id,array('lastLogin'=> date("Y-m-d H:i:s")));



try putting




if (!$user_log->save())

     var_dump($user_log->getErrors()

);



THis is working, update lastLogin time in User table.

User::model()->updateByPk($user->id,array(‘lastLogin’=> date(“Y-m-d H:i:s”)));

I got this error

array(2) { ["event"]=> array(1) { [0]=> string(22) "Event cannot be blank." } ["detail"]=> array(1) { [0]=> string(23) "Detail cannot be blank." } }