简体   繁体   English

设置cookie而不转义特殊字符

[英]Set cookie without escaping special characters

I've discovered that setcookie() function escapes special characters, such as quotes. 我发现setcookie()函数会转义特殊字符,例如引号。 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. 此外,正如您所看到的,我不得不删除'alt'属性,因为我无法正确删除其中的转义符号。 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. 对此我唯一的另一个解决方案是将编码的字符串写入cookie,然后在获取cookie时对其进行解码。 Will this solution be faster than str_replace()? 这个解决方案会比str_replace()更快吗? Are there any better methods? 有没有更好的方法?

Thanks for your time. 谢谢你的时间。

If you are storing large amounts of data in cookies, then you might want to consider using sessions instead. 如果要在cookie中存储大量数据,则可能需要考虑使用会话。 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 http://www.php.net/manual/en/intro.session.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM