Jump to content

[SOLVED] URL-safe Serialization


Zaxnyd

Recommended Posts

I need to serialize an object and pass it via URL.

 

I've tried this, and many variations of it, but nothing seems to pass seemlessly in all cases:

 

	$sSerialized = serialize($myObj);  
	$sSerialized = base64_encode($sSerialized);
	$sSerialized = urlencode($sSerialized);

	//pass via url, then on next page:

	$sSerialized = urldecode($sSerialized);
	$sSerialized = base64_decode($sSerialized);
	$myObj= unserialize($sSerialized);

 

Seems like this should be a very easy thing to do, but I can't seem to get the hang of it.

 

Thanks in advance.

Link to comment
Share on other sites

Why can't you pass it in a session?

 

It's actually being passed via Ajax, so I don't think session would work.

 

Is there no way to make a guaranteed URL-safe, string version of any given object without the corruption of loss of the serialized data?

Link to comment
Share on other sites

I had my encode/decode functions output the data at each stage of the transformation.

 

I had it encode the data, then decoded it locally.  Then it passed the data via URL to be decoded again.

 

Most notable are the differences between the "As received" for the pre-pass and post-pass decodings.

 

Encoding...



this:
Object id #6

serialize:
O:10:"Classified":9:{s:2:"ID";s:1:"6";s:6:"userID";s:1:"1";s:4:"date";s:10:"0000-00-00";s:7:"listing";s:6:"Wanted";s:8:"category";s:8:"Antiques";s:7:"subject";s:7:"Testing";s:4:"body";s:191:"
This is a p 

This is a list 
I am javascript ";s:8:"approved";N;s:5:"table";s:20:"wolfcreek_classified";}

base64_encode:
TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw+DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ==

urlencode
TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw%2BDQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ%3D%3D
--------------------------------------------------------------------------------
Decoding...



As received:
TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M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:{s:2:"ID";s:1:"6";s:6:"userID";s:1:"1";s:4:"date";s:10:"0000-00-00";s:7:"listing";s:6:"Wanted";s:8:"category";s:8:"Antiques";s:7:"subject";s:7:"Testing";s:4:"body";s:191:"
This is a p 

This is a list 
I am javascript ";s:8:"approved";N;s:5:"table";s:20:"wolfcreek_classified";}

unserialize:
Object id #22

--------------------------------------------------------------------------------
Pass via URL

--------------------------------------------------------------------------------
Decoding...



As received:
TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw+DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ==

urldecode:
TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ==

base64_decode:


unserialize:

Link to comment
Share on other sites

Solved it.  It was the pesky plus sign that was doing it.  So I just did an eregi_replace with a placeholder and it works wonderfully.  Here's the class I made.  Some of you might appreciate it:

 

class url_serializable {
function loadFromSerial($sSerial) {
	$urldecode = urldecode($sSerial);
	$urldecode = eregi_replace("--PLUS--", "\+", $urldecode);
	$base64_decode = base64_decode($urldecode);
	$unserialize = unserialize($base64_decode);
	return $this->loadFromObject($unserialize);
}
function sSerialized() {
	$sSerialized = $this;
	$sSerialized = serialize($sSerialized);
	$sSerialized = base64_encode($sSerialized);
	$sSerialized = eregi_replace("\+", "--PLUS--", $sSerialized);
	$sSerialized = urlencode($sSerialized);
	return $sSerialized;
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.