Search code examples
phpsessionserializationspecial-characters

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. E.g. 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. Are there restrictions to storing in sessions, like only ASCII chars? Do I have to convert the whole string in another format, e.g. Base64? Or how to handle this best? Thanx.


Solution

  • Thanks a lot to Sammitch for the note in his comment: problem was the pre-serialization. If I just assign the object to the $_SESSION like

    $_SESSION['User'] = $this;
    

    it is no problem if a field of the object contains those characters