phpcookiessetcookie

Set cookie without escaping special characters


I've discovered that setcookie() function escapes special characters, such as quotes. I've solved this problem by cleaning out those trash symbols:

$new_avt = str_replace("\'","",$_COOKIE['avatar']);
$new_avt = str_replace('alt=','',$new_avt);

But I find this solution ugly. Furthermore, as you may see, I had to remove 'alt' property, for I couldn't correctly get rid of escape symbol inside it. The only other solution to this I've came up to, is to write an encoded string to cookie, and then decode it when cookie's fetched. Will this solution be faster than str_replace()? Are there any better methods?

Thanks for your time.


Solution

  • If you are storing large amounts of data in cookies, then you might want to consider using sessions instead. This stores the data on the server, rather than sending it to/from the client on each request. Any variable that can be serialized can be stored in a session variable. See http://www.php.net/manual/en/intro.session.php