The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Is serialized
Discuss Is serialized in the PHP Development forum on Dev Shed. Is serialized PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 3rd, 2013, 08:15 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
|
Is serialized
Is there a way to detect if a string is a serialized object/array ?
eg
PHP Code:
if(is_serialized($str)) {
$val = unserialize($str);
}
if there isn't then I may have to write my own, and knowing my ambitions it will end up trying to detect php's serialize, json and xml. Knowing my skills, this may not fully work!!!
|

January 3rd, 2013, 08:30 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
where do these strange strings come from?
If it's a leftover of some buggy program or something and there's really no better solution, I'd simply call unserialize() and check if the return value is not "false". But you have to make sure the string isn't actually a serialized "false" (b:0  -- the genius of PHP strikes again.
Of course that's not an elegant solution, but the problem isn't elegant, either.
I wouldn't even try to parse the strings. You'd end up rewriting complex stuff that PHP has already built in. Only do that if you're dealing with gigantic values that cannot simply be deserialized on the fly.
Last edited by Jacques1 : January 3rd, 2013 at 08:34 AM.
|

January 3rd, 2013, 08:37 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
|
It's coming from several external sources in POST requests
In one instance it's a payment gateway similar to paypal, some values are plain text scalar values, others have been serialzed using php's serialize function or equivilent
In another case it is coming from a POST request made by client side javascript and could be either xml or JSON.
However, in some cased the entire response is either JSON or XML (i use file_get_contents("php://input");) in other cases I am receiving more standard URL encoded POST data, with some values serialised or JSON'd
I am attempting to consolidate the code, so I only have to maintain one receiver, rather than 3...if I could get the code to decide what to run then future development would be easier.
I could look at the first few characters and attempt to make a decision, decode and compare to the original....but it all just seems a bit messy
|

January 3rd, 2013, 09:00 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Does each value have a predefined data format? If that's the case, then why you don't you use this information to select the correct decoding function?
I'm not really sure what you're trying to do. If you're trying to write a generalized function, which automatically determines the data format for each case, then this is not a good idea. Set the format explicitly.
Or is the data format of the values not even known beforehand? That would be a real mess ...
|

January 3rd, 2013, 09:09 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
Quote: | Originally Posted by Jacques1 Does each value have a predefined data format? |
Some, but not all
Quote: | Originally Posted by Jacques1 Or is the data format of the values not even known beforehand? That would be a real mess ... |
most of the time this is true
The problem is that the data is not being properly handled/formatted by the 3rd parties.
for example, the serialized strings could easily be url encoded, but it's serialized instead, eg $_POST['response'] is a serilaized string as its value, rather than being an array like
$_POST['response']['amount'] or $_POST['response']['auth_number']
testing if something is an array is nice and easy...testing if something was an array....
|

January 3rd, 2013, 09:16 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Well, in this case either use unserialize() as suggested above or your idea of checking only the first few characters.
If you need the unserialized values, anyway, then unserialize() would actually be a pretty good way of dealing with this.
|

January 3rd, 2013, 09:33 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
|
I'm hoping to avoid unnecessarily calling unserialize() on values which are not serialized
|

January 3rd, 2013, 09:59 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Why? If it's not serialized, the function will simply fail after the first few characters.
If you don't like that, then use your idea. Give up hope of finding a perfect solution for this. The application you're getting the values from is simply broken, so all you can do is try to find an acceptable workaround. You have two sensible options. Both aren't elegant, but they'll work.
Yes, you could dig deep into the PHP source code, look up the exact serialize() logic and then write a parser. But how would that improve the application? It would be just as broken as it was before, but you'll have wasted a lot of time.
|

January 3rd, 2013, 10:20 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
A quick check would be to see if the string stars with \w+:, so:
PHP Code:
if ( preg_match("/^\w+:/", $yourString)
&& $a = unserialize($yourString) && $a !== false )
-Dan
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|