user739350
user739350

Reputation: 113

How do I set a cookie?

How can I set a cookie and retrieve the cookie value in Drupal 8?

In plain PHP, I could use setcookie('country', $country, time() + (86400 * 30), "/");, but I think there is a better way, for Drupal 8.

Upvotes: 2

Views: 8354

Answers (1)

Alexander
Alexander

Reputation: 12775

Take a look at user_cookie_save() which accepts an array of values.

user_cookie_save(['your_cookie' => "your_value"])

Creates a Drupal.visitor.your_cookie cookie:

Upvotes: 4

Related Questions