简体   繁体   English

序列化对象中的特殊字符正在破坏PHP会话

[英]Special chars in serialized object are destroying PHP session

After login of the user I serialize the user object and store it in his session just like this 登录用户后,我将用户对象序列化并将其存储在他的会话中,就像这样

$_SESSION['User'] = serialize($this);

so I have faster access to his data in further steps. 因此,在以后的步骤中,我可以更快地访问他的数据。

Now, sometimes users can't stay logged in because their session is immediatly destroyed with the next page request. 现在,有时用户无法保持登录状态,因为下一页请求会立即破坏其会话。 I found that it is because of special chars the user stored in his data. 我发现这是由于用户将特殊字符存储在他的数据中。 Eg in one case there were some sweet hards in a notice 例如,在一个案例中,通知中有一些甜言蜜语

s:72:"♥ Aufsicht ♥ <br>...;"

In another case a phone number was copied from Apple contacts and so on. 在另一种情况下,从Apple联系人中复制了电话号码,依此类推。 Are there restrictions to storing in sessions, like only ASCII chars? 在会话中是否有存储限制,例如仅ASCII字符? Do I have to convert the whole string in another format, eg Base64? 我是否必须将整个字符串转换为其他格式,例如Base64? Or how to handle this best? 或如何最好地处理? Thanx. 谢谢

Thanks a lot to Sammitch for the note in his comment: problem was the pre-serialization. 非常感谢Sammitch在评论中的注释:问题是预序列化。 If I just assign the object to the $_SESSION like 如果我只是将对象分配给$_SESSION

$_SESSION['User'] = $this;

it is no problem if a field of the object contains those characters 如果对象的字段包含这些字符是没有问题的

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

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