In controller I have sent cookies like this:
Yii::$app->response->cookies->add(new yii\web\Cookie(['name' => 'user.name', 'value' => 'Jack Black']));
and
Yii::$app->response->cookies->add(new yii\web\Cookie(['name' => 'user-login', 'value' => 'jack']));
Then to get cookies in view I use code like this:
Yii::$app->request->cookies->getValue('user.name');
and
Yii::$app->request->cookies->getValue('user-login');
I waiting for 'Jack Black' and 'jack'.
I receive a NULL value and 'jack'.
Why dot symbol is not allowed in cookie on getting?
| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 5.4
| Operating system | Ubuntu
What do you have in $_COOKIES?
Cause this
Yii::$app->response->cookies->add(new yii\web\Cookie(['name' => 'user.name', 'value' => 'Jack Black']));
sets cookie to response. Nothing more.
And you read cookies from request.
Request cookies and response cookies are different cookie bags.
@dmirogin I think he actually means reading cookie in a view in the next request, not the same one. Am I right, @papalapa?
@dmirogin, @samdark is right about setting and getting cookies.
The browser cookies after this manipulations has both variables: user.name and user-login, but on getting values I have this:
var_dump(Yii::$app->request->cookies->getValue('user-login')); // jack
var_dump(Yii::$app->request->cookies->getValue('user.name')); // NULL
I think problem is in 芦.禄 symbol in cookie variable name.
Not sure we should handle it.
Oh, it is deeper than seems. I have never thinking about global variable names. Thank you @samdark.
Either we should convert dots to underscores behind the scenes or throw an exception for cookies with dots in their names.
I think throwing an exception will be a better way to handle it.
Yes. Agree.
I think throwing an exception will be a better way to handle it.
Disagreed. There are many symbols, which are not allowed in the Cookie name: spaces =, ; and so on. Should we check them too?
Besides PHP does not put any restriction on setting cookie with dot in the name. The only problem is that appears in $_COOKIE with dots been replaced by underscore.
Also this behavior may be fixed in the future PHP versions as 'register_globals' directive has been removed long ago.
I do not think any actions should be performed.
Makes sense.