What namespace should be used for creating cookies?

I want to set and get a cookie using Cookie::get(‘cookie_name’); and Cookie::make(‘cookie_name’, ‘value’, $expiry). what namespace do I ned to use to use it? Can I also just call it in code or do I need to return it to the page in a response to create the cookie as I do the make and right away do the get and it returns null.

Hi,

You can queue your cookie and it will be sent in the response :

Cookie::queue($name, $value, $minutes);

You can also serialize the value if you want to store more than one value in the same cookie (ex: a cookie named “user_preferences” that expires after 6 months).

And then, you can get the cookie value using :

$value = Cookie::get('name');

For the namespace, if your website has a cookie called user_preferences and another website has the same cookie name, it shouldn’t be a problem:

Due to the Same Origin Policy, client scripts (e.g. JavaScript) should only be able to read and write cookies belonging to the current domain. So for the client script interacting with the cookie, there is only one cookie per name - it should not even be aware of cookies for other domains

1 Like