★ wanayoo — archive 1999 http://www.php.net/manual/function.serialize.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Misc.
Quick Reference
Misc.
* connection_aborted
* connection_status
* connection_timeout
* define
* defined
* die
* eval
* exit
* func_get_arg
* func_get_args
* func_num_args
* function_exists
* get_browser
* ignore_user_abort
* iptcparse
* leak
* pack
* register_shutdown_function
* serialize
* sleep
* uniqid
* unpack
* unserialize
* usleep
Manual: serialize
View the source code for this pageSearch the site



Previous page
 register_shutdown_function
sleep 
Next page


serialize

serialize -- Generates a storable representation of a value.

Description

string serialize(mixed value);

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 the types integer, double, string, array (multidimensional) and object (object properties will be serialized, but methods are lost).

Example 1. Serialize() example

  1 
  2 // $session_data contains a multi-dimensional array with session
  3 // information for the current user.  We use serialize() to store
  4 // it in a database at the end of the request.
  5 
  6 $conn = odbc_connect ("webdb", "php", "chicken");
  7 $stmt = odbc_prepare ($conn,
  8                       "UPDATE sessions SET data = ? WHERE id = ?");
  9 $sqldata = array (serialize($session_data), $PHP_AUTH_USER);
 10 if (!odbc_execute ($stmt, &$sqldata)) {
 11     $stmt = odbc_prepare($conn,
 12                          "INSERT INTO sessions (id, data) VALUES(?, ?)");
 13     if (!odbc_execute($stmt, &$sqldata)) {
 14         /* Something went wrong.  Bitch, whine and moan. */
 15     }
 16 }
 17       


User Contributed Notes: serialize


cmv@easyDNS.com
25-Mar-1999 06:34
Based on my experience:<p> If you are storing serialized data in a database, you need to serialize() the data, *then* addslashes(), then stick it in the DB.

When you pull it out, stripslashes() (if you don't automatically do it), then unserialize().

If you are passing serialized data between pages in hidden form fields (or in a query string), you need to serialize() the data, then *urlencode()* it, then put it in the hidden field.

When you get to the next page, urldecode() it, then unserialize().



cmv@easyDNS.com
26-Mar-1999 12:23
<B>Oops!</B>

If you are passing serialized data in a <i>query string</i>, you <B>don't</B> need to urldecode() it on the following page. Just unserialize().

Hidden <i>form fields</i>, however, need to be urldecode()-ed then unserialize()-ed as stated above.



mcdermam@potsdam.edu
23-Apr-1999 01:18
I have been struggling with passing arrays and objects as form variables and/or query strings and have noted some things which seem to be (unfortunately) true: 1) passing arrays or objects as query strings after serializing/urlencoding passes the exact same string when using forms and when using query strings, but the results are not the same. When using forms the string passed decodes and deserializes properly for arrays, but the query string counterpoint fails. The passed string serializes into an array, but the elements are not accessible, at least not in a standard way. e.g. echo"$passed_array[index or key]" gives an error message about scalars used as arrays.


kip@axl.net
11-Feb-2000 12:59
I've also been having some difficulty in using this function with an object. I have a form that posts variable to a page which declares an instance of a class, sets the class instance's internal variables from global form variable, then attempts to serialize the object, urlencode it, pass it in the url to another page, and I get the error "The page cannot be displayed" I'm beginning to think it may be the size of the object I am passing. When I do this same thing with a simple integer, there are no errors. The code I'm using is: $object = new class; $object->read(); // stuffs object with vars from $GLOBALS[] $serialized_object = serialize($object); $urlencoded_object = urlencode($serialized_object); Header("Location:/nextpage.phtml?object_pass=$urlencoded_object"); On the next page I have $serialized_object = urldecode($object_pass); $object = new class; $object = unserialize($serialized_object); Like I said, if I use the same procedure without classes, everything goes fine...


kip@axl.net
11-Feb-2000 03:42
Okay, I realize the code I entered above did not format as I meant, but a new issue has come to light: this routine executes flawlessy in Netscape Navigator 4.61 for Windows 98, but IE 5 gives me the error above (The page cannot be displayed) Has anyone ran into an error like this before?


MarkRoedel@letu.edu
22-Feb-2000 04:58
A call to serialize() appears to mess with the array's internal pointer. If you're going to be walking through your array after serializing it, you'll want to make a call to reset() first.


nargy@paris.fm
10-May-2000 05:29
About serialized objects (PHP3): when calling unserialize() on an object transforms it into an array !


nargy@yahoo.com
11-May-2000 07:46
About serialize/unserialize on objects:

Use this class to unserialize objects :

class Unserializable
{
  function unserialize($str)
  {
    $ary=unserialize($str);
    for(reset($ary);$kv=each($ary);)
    {
      $key=$kv[key];
      if (gettype($this->$key)!="user function")
        $this->$key=$kv[value];
    }
  }
}




 About Notes


Previous page
 register_shutdown_function
sleep 
Next page



Site Statistics


Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.