I am working on a system where I retrieve user preferences and insert them into cookies with JSON because I have a Javascript file that needs to use the value in the cookie too.
Now, when i use JSON.Stringify() with javascript i get the data just as i expect it.
However, when i go to check the cookie after it has run the PHP script i get this:
Here is my code from the php file:
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event){
if($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')){
$selectedCategories = $event->getAuthenticationToken()->getUser()->getProfile()->getCategories();
setcookie("userInterests",json_encode(get_object_vars($selectedCategories)),(time()+3*60*60*24*30));
}
}
any help would be greatly appreciated!
In my case I had multiple problems: firstly I tried to enter an object(that I thought was an array) into the cookie. This had the weird cookie as a result, because the PHP cookie only held the start string for the urlencode.
Secondly, I didn't know that cookies automatically get urlencoded in PHP so that threw me of. If I just use urldecode now, at the point where I am returning the data from the cookie. It should just pass the javascript cookie without editing it and my php cookie will be useable.