Ad

How To Get Cookie In OctoberCMS

- 1 answer

I've been made simple cookie. When user click button it save current time in cookie. My problem is that, I need to print that date and make a conditional.

I need to print cookie current time in default.htm or navbar.htm

This is what I have trying to add in

onStart() { 
   $cookie_time = htmlspecialchars($_COOKIE['get_date_time']);
}

and in navbar.htm

 {{ cookie_time }}

but it's not printed or anything. When I check in browse my site stores that cookie correctly.

Ad

Answer

Although it is not the best, the way you are getting cookie value is fine. Your problem is in how you pass the value to the twig template.

The syntax to assign to $cookie_time is wrong.
It should be like; $this['cookie_time'] = ...

Complete page code is this;

url = "/test"
==
public function onStart() {
    $this['cookie_time'] = htmlspecialchars($_COOKIE['get_date_time']);
}
==
{{ cookie_time }}

See Hello World example in October document.

Ad
source: stackoverflow.com
Ad