How to get user's IP address?


I want to store the user’s IP address upon logging in. How do I do that?

Moreover, I don’t want this to affect the update_time attribute automatically updated by CTimeStampBehavior, as I want that to track other changes to the user’s record.

Thanks for the help.

Is this what you are looking for?


Thank you, I feel stupid; I searched all over for “ip address” :P

Now, how would I go about avoiding the behavior update? Thanks again…

This is completely untested on my part, this framework is kind of huge if you need to know all of it’s workings. I’m a little vague on behaviors, so far I have only used a custom behavior when uploading files. However I think this might put you in the right direction.

Thanks for the tip, I’ll look into it… :) :)

Well, I tried disableBehavior() and couldn’t get it to work so I reread the docs on CTimestampBehavior and found out how to do it (in WebUser.php component):

public function login($identity)


	$currentUser= Users::model()->findByPk($identity->Id);

	$currentUser->last_logon = new CDbExpression('NOW()');

	$currentUser->last_ip_address = ip2long(CHttpRequest::getUserHostAddress());




	return parent::login($identity);


The above code will store the user’s logon timestamp and IP address without affecting the updated timestamp handled by the model’s CTimestampBehavior. Hope this helps someone else…

u can also display user’s ip address by: <?php echo CHttpRequest::getUserHostAddress();?>