Answer the question
In order to leave comments, you need to log in
How to set cookies in yii2 once if there are several requests at once?
Site on yii2, at the same time another site accesses it, with two or more requests. Naturally, there are no cookies in the requests, and I try to set a cookie, and before installing I check if there is a cookie
$cookies = Yii::$app->request->cookies->getValue('name');
md5(microtime() . rand(0, 1000) );
public static function setCookies(){
$cookies = Yii::$app->request->cookies->getValue('name_cookie');
if(!$cookies){
$set_cookies_date = (new DateTime())->modify('+1828 day')->format('d.m.Y H:i:s');
$ip = Yii::$app->request->userIP;
$hash = md5(microtime() . rand(0, 1000) . $ip);
$set_cookies = Yii::$app->response->cookies;
$set_cookies->add(new \yii\web\Cookie([
'name' => 'name_cookie',
'value' => $hash,
'domain' => 'site.local',
'expire' => strtotime($set_cookies_date),
]));
}
}
Answer the question
In order to leave comments, you need to log in
Check if there is a value for this cookie, and if not, then do not set.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question