Sam
Sam

Reputation: 1828

symfony 4 setting cookie -> invalid cookie name?

Trying to set a cookie and getting an error at set

Warning: Cookie names cannot contain any of the following '=,; \t\r\n\013\014'

but my name selector is ok ?

$cookie = new Cookie('selector', $cookieData, time() + 60 * 60 * 24 * 365, '/', null, false, false);        
setCookie($cookie);

Upvotes: 0

Views: 3248

Answers (1)

Chip Dean
Chip Dean

Reputation: 4302

setcookie is a PHP function that is not intended to work with the Symfony Cookie class. Technically your code should not even be working because the PHP function is actually all lowercase like this: setcookie. You should be getting an error for undefined function because setCookie is a method of the Response object. It's not global.

Anyway, If you want to use the Symfony framework you will need to set the cookie like this using the Response object:

$response->headers->setCookie($cookie);

If you want to use just PHP you would set the cookie like this:

setcookie('selector', $cookieData, time() + 60 * 60 * 24 * 365, '/');

Hope that helps!

Upvotes: 5

Related Questions