/*** This program is free software. It comes without any warranty, to* the extent permitted by applicable law. You can redistribute it* and/or modify it under the terms of the Do What The Fuck You Want* To Public License, Version 2, as published by Sam Hocevar. See* http://sam.zoy.org/wtfpl/COPYING for more details.*//*** Tests if an input is valid PHP serialized string.** Checks if a string is serialized using quick string manipulation* to throw out obviously incorrect strings. Unserialize is then run* on the string to perform the final verification.** Valid serialized forms are the following:**boolean: b:1;*integer: i:1;*double: d:0.2;*string: s:4:"test";*array: a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}*object: O:8:"stdClass":0:{}*null: N;*** @author Chris Smith* @copyright Copyright (c) 2009 Chris Smith (http://www.cs278.org/)* @license http://sam.zoy.org/wtfpl/ WTFPL* @param string $value Value to test for serialized form* @param mixed $result Result of unserialize() of the $value* @return boolean True if $value is serialized data, otherwise false*/function is_serialized($value, &$result = null){// Bit of a give away this oneif (!is_string($value)){return false;}// Serialized false, return true. unserialize() returns false on an// invalid string or it could return false if the string is serialized// false, eliminate that possibility.if ($value === 'b:0;'){$result = false;return true;}$length = strlen($value);$end = '';switch ($value[0]){case 's':if ($value[$length - 2] !== '"'){return false;}case 'b':case 'i':case 'd':// This looks odd but it is quicker than isset()ing$end .= ';';case 'a':case 'O':$end .= '}';if ($value[1] !== ':'){return false;}switch ($value[2]){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:break;default:return false;}case 'N':$end .= ';';if ($value[$length - 1] !== $end[0]){return false;}break;default:return false;}if (($result = @unserialize($value)) === false){$result = null;return false;}return true;}
Saturday, September 18, 2010
Check if variable is serialized in PHP
Want to check if a variable is serialized or not in PHP? Here you go ....
Labels:
check,
PHP developers,
serialize,
unserialize
Friday, July 30, 2010
PHP Developers
Over the years I've observed that the least attentional programmers in industry are the PHP programmers. They are just simply unaware. Their domain is Web, but they are almost blind about what's happening in world around Web. Go and ask a .NET developer, he will utter out all the changes that happened since last few versions on framework and he will also mention the anticipated changes in next version. While I'm not a great fan of .NET developers' understanding of nitty-gritty of technologies I must say - they know what they need to! And PHP developers? Well, they just don't know anything, neither they know that what to know. They would start working on any project without even understanding it, let alone giving attention to the detail - as if they are assigned to dig a hole in said time.
The poorness of development methodology of PHP programmers is second only to VB programmers. Bad programming approach can easily be seen in most of the core developments. Hack in opensource is regarded as skill.
People talk about PHP not being scalable as compared to .NET or Java. What is scalability? Scalability depends hugely on the architecture of the system and less on the platform. While .NET and Java have their inbuilt frameworks, we avoid frameworks. PHP 5 has pretty good support for OOPS and there is no reason why it should not be scalable. If it was not scalable, how Joomla is so scalable that it can meet almost any kind of requirement? Even Drupal achieves this aspect without being OOPS (however in not so pleasant way). So it's the PHP developers who let their language down.
There are reasons for this. A PHP developer is almost always on red alert state because most PHP projects are Project Based and not Prodcut Based. Industry requires output, and in PHP - fast output. There are few finance related issues in PHP platform that I would like to avoid here. But none of them should stop programmers from evolving.
Kapil Dave.
The poorness of development methodology of PHP programmers is second only to VB programmers. Bad programming approach can easily be seen in most of the core developments. Hack in opensource is regarded as skill.
People talk about PHP not being scalable as compared to .NET or Java. What is scalability? Scalability depends hugely on the architecture of the system and less on the platform. While .NET and Java have their inbuilt frameworks, we avoid frameworks. PHP 5 has pretty good support for OOPS and there is no reason why it should not be scalable. If it was not scalable, how Joomla is so scalable that it can meet almost any kind of requirement? Even Drupal achieves this aspect without being OOPS (however in not so pleasant way). So it's the PHP developers who let their language down.
There are reasons for this. A PHP developer is almost always on red alert state because most PHP projects are Project Based and not Prodcut Based. Industry requires output, and in PHP - fast output. There are few finance related issues in PHP platform that I would like to avoid here. But none of them should stop programmers from evolving.
Kapil Dave.
Subscribe to:
Posts (Atom)