Zaxnyd Posted April 24, 2007 Share Posted April 24, 2007 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 https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/ Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 what are you trying to pass? Link to comment https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-237464 Share on other sites More sharing options...
Zaxnyd Posted April 24, 2007 Author Share Posted April 24, 2007 what are you trying to pass? I'm passing an object of my own design. Link to comment https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-237470 Share on other sites More sharing options...
fert Posted April 24, 2007 Share Posted April 24, 2007 Why can't you pass it in a session? Link to comment https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-237472 Share on other sites More sharing options...
Zaxnyd Posted April 24, 2007 Author Share Posted April 24, 2007 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 https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-237482 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 Most likely there are quotes (either single or double) which are being escaped by a backslash in the receiving script. Use the function stripslashes() on the incoming string before doing the unserialize. Ken Link to comment https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-237644 Share on other sites More sharing options...
Zaxnyd Posted April 25, 2007 Author Share Posted April 25, 2007 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: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw%2BDQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ%3D%3D urldecode: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw+DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== base64_decode: 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";} unserialize: Object id #22 -------------------------------------------------------------------------------- Pass via URL -------------------------------------------------------------------------------- Decoding... As received: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw+DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== urldecode: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== base64_decode: unserialize: Link to comment https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-238466 Share on other sites More sharing options...
Zaxnyd Posted April 25, 2007 Author Share Posted April 25, 2007 I tried the ajax post method and the same conversion seems to occur. Link to comment https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-238616 Share on other sites More sharing options...
Zaxnyd Posted April 25, 2007 Author Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48530-solved-url-safe-serialization/#findComment-238640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.