M
M
Maxim2015-01-06 16:48:20
Yii
Maxim, 2015-01-06 16:48:20

What is the best way to store temporary information in Yii2?

Posts have ratings. Not only the user, but also the guest can raise the rating of the post. It is necessary to make it possible to raise the rating only once a day. If this is a user, the rating increase is controlled by the database, but what about the guests? The first thing that comes to mind is to use cookies, in the controller I do this:

if(!Yii::$app->request->isAjax)
    return json_encode(['status'=>'err_3']);

// если гость

if(!isset(Yii::$app->request->cookies['like_' . $id])) { // $id - ид поста
    Yii::$app->response->cookies->add(new \yii\web\Cookie([
        'name'=>'like_' . $id,
        'value'=>1,
        'expire'=>86400,
    ]));
    // повышаем рейтинг и возвращаем 'ok'
}

The problem is that isset(Yii::$app->request->cookies['like_' . $id]) always returns false. Where is the error? Maybe there are ways to better identify the guest?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2015-01-06
Protko @Fesor

Well, at least do not store this in cookies.

V
Vadim, 2015-01-06
@Folour

is the path the same?

P
Pavel Solovyov, 2015-01-06
@pavel_salauyou

store this data in a cache, for example, redis, set a timeout for 24 hours, the value will automatically be deleted after 24 hours, you won’t even have to do anything, just check if it is in the radish database.

A
Alexander Gubarev, 2015-01-11
@AlexGx

I would store information about the guest in the database (or cache), perhaps the format of the database table will have to be expanded (add the guest flag, or write 0 in user_id in the case of a guest) with a link to the browser and IP to avoid cheating. Also additionally hedged cookies. Cookies as the main "storage" are not quite suitable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question