L
L
lolka022018-10-10 14:53:51
Yii
lolka02, 2018-10-10 14:53:51

Why cookies don't work (Yii2)?

There is domain1.com and domain2.com
from domain1.com we send a request on domain2.com Ajax to the page /ajax/cookie

$cookies = Yii::$app->request->cookies;
        if (!$cookies->has($cookieName)) {
            Yii::$app->response->cookies->add(new \yii\web\Cookie([
                'name' => $cookieName,
                'value' => 'test',
                'expire' => time() + 3600 * 24 * 365
            ]));
        } else {
            $value = Yii::$app->response->cookies->get($cookieName);
        }

Cookies are not set, can you set them at all? if yes, what is wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-10-10
@lolka02

Good afternoon.
Tweaked your code a little.

$cookies = Yii::$app->request->cookies;
if (!$cookies->has($cookieName)) {
    Yii::$app->response->cookies->add(new \yii\web\Cookie([
        'name' => $cookieName,
        'value' => 'test',
        'expire' => time() + 3600 * 24 * 365
    ]));
} else {
    $value = $cookies->get($cookieName);
}

This sets cookies
and this reads
Difference in "response" and "request"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question