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

PHP Home Page

Manual Table of Contents
Up to Sonstiges
Quick Reference
English version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Sonstiges
* 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
* highlight_string
* highlight_file
* show_-source
Manual: serialize
View the source code for this pageSearch the site



Previous page
 register_shutdown_function
 Updated
Sat, 12 Aug 2000
sleep 
Next page


serialize

(PHP3 >= 3.0.5, PHP4 )

serialize --  Erzeugt ein speicherbares Datenformat

Beschreibung:

string serialize (mixed value)

Serialize() gibt eine Zeichenkette (String) zurück, die eine Byte-Stream entsprechende Wiedergabe von value enthält und beliebig abgespeichert werden kann.

Diese Funktion dient der Speicherung oder Übergabe von PHP-Werten, ohne dass diese ihren Wert oder ihre Struktur verlieren.

Um eine serialisierte Zeichenkette wieder als PHP-Wert verfügbar zu machen steht die unserialize()-Funktion zur Verfügung. Serialize() kann mit den Typen integer, double, string, array (mehrdimensional) und object umgehen. Beim Objekt werden die Eigenschaften serialisiert, die Methoden gehen aber verloren.

Beispiel 1. Serialize()-Beispiel:


// $session_data enthält ein mehrdimensionales Array mit
// Session-Informationen des aktuellen Benutzers. Wir benutzen
// serialize() um diese Infos am Ende der Anfrage in eine
// Datenbank zu speichern.

$conn = odbc_connect ("webdb", "php", "moorhuhn");
$stmt = odbc_prepare ($conn,
                      "UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $PHP_AUTH_USER);
if (!odbc_execute ($stmt, &$sqldata)) {
    $stmt = odbc_prepare ($conn,
                          "INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!odbc_execute($stmt, &$sqldata)) {
        /* Etwas lief schief. Fluche, heule und stöhne. */
    }
}
      


User Contributed Notes: serialize


cmv@easyDNS.com
25-Mar-1999 12:34
Based on my experience:



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
25-Mar-1999 06: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 07: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
10-Feb-2000 06: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
10-Feb-2000 09: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
21-Feb-2000 10: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 11:29
About serialized objects (PHP3):
when calling unserialize() on an object transforms
it into an array !



nargy@yahoo.com
11-May-2000 01:46
About serialize/unserialize on objects:
<PRE>
</PRE>
Use this class to unserialize objects :
<PRE>

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];
}
}
}

</PRE>



 About Notes


Previous page
 register_shutdown_function
 Updated
Sat, 12 Aug 2000
sleep 
Next page





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.