Lyels
September 8, 2011, 4:53am
1
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.
mbi
(mbi)
September 8, 2011, 5:32am
2
use the models beforSave method
mukeshsoni
(Mukeshsoni)
September 8, 2011, 7:32am
3
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.
the $_SERVER variable stores the incoming ip. You can use
$_SERVER[‘REMOTE_ADDR’];
to get the ip from which the form was submitted
dckurushin
(Diavolonok)
September 8, 2011, 9:49am
4
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
Lyels
September 8, 2011, 7:17pm
5
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…
tri
(tri - Tommy Riboe)
September 8, 2011, 7:46pm
6
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
Lyels
September 9, 2011, 6:32am
8
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.
Lyels
September 9, 2011, 7:23pm
9
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.