Home > Backend Development > PHP Tutorial > Unserialize and Autoload in php

Unserialize and Autoload in php

WBOY
Release: 2016-07-25 09:03:38
Original
982 people have browsed it
  1. $string = 'O:6:"Foobar":2:{s:3:"foo";s:1:"1";s:3: "bar";s:1:"2";}';

  2. $result = unserialize($string);
  3. var_dump($result);

  4. /*

  5. object(__PHP_Incomplete_Class)[ 1]
  6. public '__PHP_Incomplete_Class_Name' => string 'Foobar' (length=6)
  7. public 'foo' => string '1' (length=1)
  8. public 'bar' => string '2' (length =1)
  9. */
  10. ?>
Copy code

When deserializing an object, if the object’s class definition does not exist, then PHP will introduce an unfinished class Concept, namely: __PHP_Incomplete_Class. Although we have successfully deserialized at this time, we still cannot access the data in the object, otherwise the following error message will appear: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition.

This is not difficult, just do a forced type conversion and turn it into an array:

  1. $string = 'O:6:"Foobar":2:{s:3:"foo";s:1:"1";s:3 :"bar";s:1:"2";}';
  2. $result = (array)unserialize($string);
  3. var_dump($result);

  4. /*

  5. array
  6. '__PHP_Incomplete_Class_Name' => string 'Foobar' (length=6)
  7. 'foo' => string '1' (length=1)
  8. 'bar' => string '2' (length=1)
  9. * /
  10. ?>

Copy code

But if the system has Autoload activated, the situation will become more complicated. By the way: PHP actually provides a configuration option called unserialize_callback_func, but its meaning is similar to autoload. I won’t introduce it here. Let’s just talk about autoload. The example is as follows:

  1. spl_autoload_register(function($name) {
  2. var_dump($name);
  3. });
  4. $string = 'O:6:"Foobar":2:{s:3: "foo"; s: 1: "1"; s: 3: "bar"; s: 1: "2"; }';
  5. $result = (array)unserialize($string);
  6. var_dump($result) ;
  7. ?>
Copy code

Execute the above code and you will find that spl_autoload_register is triggered. Most of the time this makes sense, but if you encounter an improperly defined spl_autoload_register, it will be tragic, such as the following This code:

  1. spl_autoload_register(function($name) {
  2. include "/path/to/{$name}.php";
  3. });
  4. $string = 'O:6:"Foobar ": 2: {s: 3: "foo"; s: 1: "1"; s: 3: "bar"; s: 1: "2"; }';
  5. $result = (array)unserialize($ string);
  6. var_dump($result);
  7. ?>
Copy code

No doubt, because the class definition file cannot be found, an error is reported! It is definitely possible to change spl_autoload_register, but the premise is that you can change it. If third-party code is involved, we cannot make the decision without authorization. At this time, we need a way to allow unserialize to bypass autoload. The simplest way is to add the class we need FAKE out:

  1. spl_autoload_register(function($name) {
  2. include “/path/to/{$name}.php”;
  3. });
  4. class Foobar {} // Oh, Shit!
  5. $string = 'O:6:"Foobar":2:{s:3:"foo";s:1:"1";s:3:"bar";s:1:"2";}' ;
  6. $result = (array)unserialize($string);
  7. var_dump($result);
  8. ?>
Copy code

I have to say that the above code is really rubbish. To provide you with one person I wrote:

  1. spl_autoload_register(function($name) {
  2. include “/path/to/{$name}.php”;
  3. });

  4. $string = 'O:6:"Foobar":2:{s:3:"foo";s:1:"1";s:3:"bar";s:1:"2" ;}';

  5. $functions = spl_autoload_functions();
  6. foreach ($functions as $function) {
  7. spl_autoload_unregister($function);
  8. }

  9. $result = (array)unserialize($ string);

  10. foreach ($functions as $function) {

  11. spl_autoload_register($function);
  12. }
  13. var_dump($result);
  14. ?>
Copy code

Although there is a bit more code, at least there is no FAKE class, which makes it look more comfortable.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template