[SOLVED] Log NewRcord into database with IP address

I have a simple CRUD form setup for members. When the admin submits the form of the member I would like to save his/her IP address into a database or file.

How would I go about this? Any help is appreciated.

Here’s the _form.php file.


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'member-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'rank'); ?>

		<?php echo $form->textField($model,'rank',array('size'=>10,'maxlength'=>10)); ?>

		<?php echo $form->error($model,'rank'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div>

I’m assuming I would add getUserHostAddress() somewhere. But I don’t know where.

use the models beforSave method

the $_SERVER variable stores the incoming ip. You can use

$_SERVER[‘REMOTE_ADDR’];

to get the ip from which the form was submitted

There is a method for this in CHttpRequest… (suggest override it cause its not that good) , but


Yii:app()->request->userHostAddress;

http://www.yiiframework.com/doc/api/1.1/CHttpRequest#userHostAddress-detail

Thanks for all of the comments… is there a method which can insert this into a file now? Like some sample code to go on? hmm…

protected/config/main.php




    'components'=>array(

      ...

      'log'=>array(

        'routes'=>array(

          ...

          array(

            'class'=>'CFileLogRoute',

            'logFile'=>'other.log',

            'categories'=>'my_category.*',

          ),

          ...



Usage:




Yii::log('some message', 'info', 'my_category');



/Tommy

Thank you Mr. Tommy.

Ok so I added the code, but beforeSave() didn’t work so I used afterSave(). So I went to my runtime folder and checked, the only file that was updated was the application.log.

Well I thought this was better then nothing until I was all it was, was "Yii:app()->request->userHostAddress;" repeated in the file. No IP address.

I’ve since setup Yii-User extension and was wondering if this would make it easier to grab the IP address when the user logs in.

Was able to handle this in the _form.php with the following, friends:


<div class="row">

		

<?php echo $form->labelEx($model,'IP'); ?>

		

<?php echo $form->hiddenField($model,'ip',array('value'=>Yii::app()->request->userHostAddress,)); ?>

		

<?php echo $form->error($model,'IP'); ?>

		

	

	</div>

Simply have in database the “ip” field and of course controller set with it. :)

For Yii Log you can visit this

http://php-google-blog.blogspot.com/2014/01/error-logs-save-in-db-in-yii.html