Page 1 of 1

Session/serialization problem

Posted: Mon Jul 18, 2005 10:29 pm
by mhuggins
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.

Code: Select all

<?
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;
	}
}
?>

Code: Select all

<?
require_once(&quote;Account.inc&quote;);
$account = new Account();

$account->setUsername(&quote;mhuggins&quote;);
$account->setFirstName(&quote;Matt&quote;);
$account->setLastName(&quote;Huggins&quote;);
$account->setEmail(&quote;mhuggins@udel.edu&quote;);

print(serialize($account));
?>
The output in the browser appears similar to the following (except that the "\0" I show here actually displays as a square-ish character):

O:7:"Account":4:{s:17:"\0Account\0username";s:8:"mhuggins";s:18:"\0Account\0firstName";s:4:"Matt";s:17:"\0Account\0lastName";s:7:"Huggins";s:14:"\0Account\0email";s:17:"mhuggins@udel.edu";}

Thanks for any help anyone can offer...I've never encountered this before, and I can't figure out what's wrong. I really appreciate it![/b]

Posted: Tue Jul 19, 2005 1:59 am
by delorian
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 :P :)

Posted: Tue Jul 19, 2005 3:06 pm
by mhuggins
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 :P :)
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?

Posted: Tue Jul 19, 2005 5:00 pm
by mhuggins
So I guess I've stumped everyone??

Posted: Tue Jul 19, 2005 5:20 pm
by mhuggins
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.

Bug details:
http://bugs.php.net/bug.php?id=26737&edit=3

Latest PHP Snapshots:
http://snaps.php.net

Posted: Tue Jul 19, 2005 5:23 pm
by onion2k
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.