Unserialize and Autoload in php

Source: Internet
Author: User
Tags autoload
Unserialize and Autoload in php

  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. ?>

When deserializing an object, if the class definition of the object does not exist, PHP will introduce an unfinished class concept, that is, __php_incomplete_class. although deserialization is successful, however, you still cannot access The data in The object. Otherwise, The following error message is displayed: 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. you only need to make a forced type conversion and change it to 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. ?>

However, if the system activates Autoload, the situation will become more complex. By the way, PHP actually provides a configuration option named unserialize_callback_func, which means similar to autoload. we will not introduce it here. let's talk about autoload, for example:

  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. ?>

Execute the above code and you will find that spl_autoload_register is triggered, which makes sense most of the time. However, if spl_autoload_register is improperly defined, it will be miserable. for example, the following 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. ?>

Undoubtedly, an error is reported because the class definition file cannot be found! Modify the spl_autoload_register command, but the premise is that you can modify it. if it involves third-party code, we cannot make the decisions without authorization. in this case, we need a way to allow unserialize to bypass autoload, the simplest method is to extract the FAKE class we need:

  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. ?>

I have to say that the above code is really spam. I will provide you with the following information:

  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. ?>

Although the code is a little more, but at least there is no FAKE class, it seems more comfortable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.