PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm having a problem with serializing objects to store in a session. When I serialize, it appears like there are null characters in the strings, resulting in the data being truncated when I attempt to add it to the session.
<?
class Account {
private $username = NULL;
private $firstName = NULL;
private $lastName = NULL;
private $email = NULL;
public function __construct() {}
public function setUsername($username) {
$this->username = $username;
}
public function getUsername() {
return $this->username;
}
public function setFirstName($firstName) {
$this->firstName = $firstName;
}
public function getFirstName() {
return $this->firstName;
}
public function setLastName($lastName) {
$this->lastName = $lastName;
}
public function getLastName() {
return $this->lastName;
}
public function setEmail($email) {
$this->email = $email;
}
public function getEmail() {
return $this->email;
}
}
?>
The output does not have any whitespaces that you did not provided. Look at the result when you access the script through the command line it is a great serialized object in a string
delorian wrote:The output does not have any whitespaces that you did not provided. Look at the result when you access the script through the command line it is a great serialized object in a string
But it does have characters I did not provide. There are null "\0" characters in the object's properties that I never provided, and yet they are being displayed as part of the serialized data. When I try to set $_SESSION["whatever"] to the object, it results in a problem where the serialized data is being truncated at the first null character (as a null character in a string tends to represent the end of the string).
Why are these null characters appearing upon serialization, and how do I prevent it?
Okay, I've found the answer myself. It appears that this is actually a bug in PHP. I'm downloading the latest Windows CVS now to try to resolve this issue.
I think it's an issue with PHP5 not being able to serialize objects properly. http://www.bigbold.com/snippets/tags/php5 .. I converted the code to PHP 4 (not got 5 installed) and it works fine.