Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

unserialize

Posted on 2009-12-27
7
Medium Priority
?
402 Views
Last Modified: 2012-05-08
$this->txndata = unserialize($d['data']);

what does unserialize mean
0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 26129364
Imagine you need to return or pass a complex object for example passing an array (which contain multiple value) to another script or page , you use serialize the array.
Other side, when you receive an serialized object you need to unserialize it to use it.

$d['data'] represent/contain a serialized object and you put it unserialized version in the var $this->txndata
0
 
LVL 8

Expert Comment

by:kumaranmca
ID: 26130311
Greetings All,

serialize() and unserialize really great functions in PHP.

serialize()

serialize() returns a string containing a byte-stream  representation of value that can be stored anywhere.  
This is useful for storing or passing PHP values around without losing their  type and structure.  
To make the serialized string into a PHP value again, use unserialize(). serialize() handles all types, except the resource-type. You can even serialize()  arrays that contain references to itself. References inside the array/object you  are serialize()ing will also be stored.

When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object  to do any last minute clean-up, etc. prior to being serialized. Likewise, when  the object is restored using unserialize() the __wakeup() member  function is called.

unserialize()

unserialize() takes a single serialized variable (see  serialize()) and  converts it back into a PHP value. The converted value is returned, and can be a  boolean, integer, float, string, array or object. In case the passed  string is not unserializeable, FALSE is returned and  E_NOTICE is issued.  Warning:  
FALSE is returned both in the case of an error  and if unserializing the serialized FALSE value. It  is possible to catch this special case by comparing str with serialize(false) or by  catching the issued E_NOTICE.  unserialize_callback_func directive:  
It's possible to set a callback-function which will be called, if an  undefined class should be instantiated during unserializing. (to prevent getting  an incomplete object "__PHP_Incomplete_Class".) Use your  php.ini, ini_set() or .htaccess to  define 'unserialize_callback_func'. Everytime an undefined class should be  instantiated, it'll be called. To disable this feature just empty this setting.  Also note that the directive unserialize_callback_func directive became  available in PHP 4.2.0.  
If the variable being unserialized is an object, after successfully  reconstructing the object PHP will automatically attempt to call the __wakeup() member function (if it exists).

0
 
LVL 8

Expert Comment

by:kumaranmca
ID: 26130323
Greetings

serialize():-  compares the array/text values in junk/encrypt format.

unserialize():-  Extract the encrypted/compress the array/text values.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rgb192
ID: 26132145
what could be an example of which each one is used

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26134323
rgb192:  I am going to save you a million points here at EE.  You can find the descriptions of the functions on the PHP web site, along with examples and user-contributed notes.  It is one of the best online manuals in the history of computing.  See the man page here:

http://us3.php.net/manual/en/function.unserialize.php

How to learn about "serialize?"  Use the search box at the upper right of each page of the PHP.net online manual.

You should ALWAYS look up EVERY PHP function before you use it in your code.  No exceptions.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 26134373
Sample code to show how each is used...

best regards, ~Ray
<?php // RAY_serialize_unserialize_2.php
error_reporting(E_ALL);
echo "<pre>";

// MAN PAGE: http://us3.php.net/manual/en/function.unserialize.php
// MAN PAGE: http://us3.php.net/manual/en/function.serialize.php

// CREATE AN ARRAY
$old = array();

// ADD TWO THINGS TO THE ARRAY
$old["uid"] = 'Ray';
$old["pwd"] = 'Paseur';

// SHOW THE ARRAY
var_dump($old);

// SERIALIZE THE ARRAY
$string = serialize($old);

// SHOW THE SERIALIZED ARRAY
var_dump($string);

// UNSERIALIZE THE STRING INTO A NEW ARRAY
$new = unserialize($string);

// SHOW THE NEW ARRAY
var_dump($new);

// DOES THE BEFORE AND AFTER MATCH?
if ($new == $old) echo "\nMATCH!";

Open in new window

0
 
LVL 1

Author Closing Comment

by:rgb192
ID: 31670288
after trying code, i think i understand
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question