Answer the question
In order to leave comments, you need to log in
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'
}
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question