P
P
Petr Ponomarev2016-11-23 17:04:43
Yii
Petr Ponomarev, 2016-11-23 17:04:43

How to write the user's ip address to the database in Yii2 during registration?

How to write the user's ip address to the database in Yii2 during registration?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-11-23
@Petrdis

You can get the user's IP address from the application's Request object, for example:
Yii::$app->request->userIP
and then write it to your user model, save it, etc.

A
Andrew, 2016-11-23
@mhthnz

$model = new User(['ip' => Yii::$app->request->userIP]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
......
}

or before saving
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    $model->ip = Yii::$app->request->userIP;
    $model->save(false);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question