Session/serialization problem

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!

Moderator: General Moderators

Post Reply
mhuggins
Forum Newbie
Posts: 10
Joined: Mon Jul 18, 2005 10:23 pm
Location: Delaware, USA

Session/serialization problem

Post 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]
Last edited by mhuggins on Tue Jul 19, 2005 4:59 pm, edited 3 times in total.
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post 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 :)
mhuggins
Forum Newbie
Posts: 10
Joined: Mon Jul 18, 2005 10:23 pm
Location: Delaware, USA

Post 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?
mhuggins
Forum Newbie
Posts: 10
Joined: Mon Jul 18, 2005 10:23 pm
Location: Delaware, USA

Post by mhuggins »

So I guess I've stumped everyone??
mhuggins
Forum Newbie
Posts: 10
Joined: Mon Jul 18, 2005 10:23 pm
Location: Delaware, USA

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply