I’m trying to set a cookie in a website i’m working in with codeigniter 4 right now.
The idea is to show a toast asking for user consent to set cookies, and if the user agrees with it I create a cookie to sabe the last visit datetime. I also want that once the user agrees or rejects it the toast will close and not open again even if the user opens another page in my site.
I tried in many ways but it didn’t work. I managed to create the cookie but without the consent in localhost, but it’s not what I want. Here is the controller code:
public function index()
{
$consentimentoCookies = 'concedido';
if($consentimentoCookies == 'concedido') {
setcookie('dataHoraUltimoAcesso', date('d-m-Y---h-i-s'));
}
return renderizaPagina('index');
}
I tried a toast with 2 buttons, one to accept and another to reject the cookies. But i couldn’t make it work properly. I expect that the cookie will only be created if the user agreed.