1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.unserialize" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>unserialize</refname>
<refpurpose>
Creates a PHP value from a stored representation
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>unserialize</methodname>
<methodparam><type>string</type><parameter>data</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>[]</initializer></methodparam>
</methodsynopsis>
<simpara>
<function>unserialize</function> takes a single serialized variable and
converts it back into a PHP value.
</simpara>
<warning>
<para>
Do not pass untrusted user input to <function>unserialize</function> regardless
of the <parameter>options</parameter> value of <literal>allowed_classes</literal>.
Unserialization can result in code being loaded and executed due to object
instantiation and autoloading, and a malicious user may be able to exploit
this. Use a safe, standard data interchange format such as JSON (via
<function>json_decode</function> and <function>json_encode</function>) if
you need to pass serialized data to the user.
</para>
<para>
If you need to unserialize externally-stored serialized data, consider using
<function>hash_hmac</function> for data validation. Make sure data is
not modified by anyone but you.
</para>
</warning>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>data</parameter></term>
<listitem>
<para>
The serialized string.
</para>
<para>
If the variable being unserialized is an object, after successfully
reconstructing the object PHP will automatically attempt to call the
<link linkend="object.unserialize">__unserialize()</link> or <link linkend="object.wakeup">__wakeup()</link> methods (if one exists).
</para>
<para>
<note>
<title>
<link linkend="ini.unserialize-callback-func">unserialize_callback_func</link>
directive
</title>
<para>
The callback specified in the
<link linkend="ini.unserialize-callback-func">unserialize_callback_func</link>
directive is called when an undefined class is unserialized.
If no callback is specified, the object will be instantiated as
<classname>__PHP_Incomplete_Class</classname>.
</para>
</note>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<para>
Any options to be provided to <function>unserialize</function>, as an
associative array.
</para>
<table>
<title>Valid options</title>
<tgroup cols="3">
<thead>
<row>
<entry>Name</entry>
<entry>Type</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>allowed_classes</literal></entry>
<entry><type>mixed</type></entry>
<entry>
<simpara>
Either an <type>array</type> of class names which should be
accepted, &false; to accept no classes, or &true; to accept all
classes. If this option is defined and
<function>unserialize</function> encounters an object of a class
that isn't to be accepted, then the object will be instantiated as
<classname>__PHP_Incomplete_Class</classname> instead.
</simpara>
<simpara>
Omitting this option is the same as defining it as &true;: PHP
will attempt to instantiate objects of any class.
</simpara>
</entry>
</row>
<row>
<entry><literal>max_depth</literal></entry>
<entry><type>int</type></entry>
<entry>
<simpara>
The maximum depth of structures permitted during unserialization,
and is intended to prevent stack overflows. The default depth limit
is <literal>4096</literal> and can be disabled by setting
<literal>max_depth</literal> to <literal>0</literal>.
</simpara>
</entry>
</row>
</tbody>
</tgroup>
</table>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
The converted value is returned, and can be a <type>bool</type>,
<type>int</type>, <type>float</type>, <type>string</type>,
<type>array</type> or <type>object</type>.
</para>
<para>
In case the passed string is not unserializeable, &false; is returned and
<constant>E_WARNING</constant> is issued.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Objects may throw <classname>Throwable</classname>s in their unserialization handlers.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.3.0</entry>
<entry>
Now emits <constant>E_WARNING</constant> when the input string has unconsumed data.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
Now emits <constant>E_WARNING</constant> when the passed string is not unserializeable;
previously <constant>E_NOTICE</constant> was emitted.
</entry>
</row>
<row>
<entry>7.4.0</entry>
<entry>
Added the <literal>max_depth</literal> element of
<parameter>options</parameter> to set the maximum depth of structures permitted during unserialization.
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
The <literal>allowed_classes</literal> element of
<parameter>options</parameter>) is now strictly typed, i.e. if anything
other than an <type>array</type> or a <type>bool</type> is given,
<function>unserialize</function> returns &false; and issues an
<constant>E_WARNING</constant>.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>unserialize</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
// Here, we use unserialize() to load session data to the
// $session_data array from the string selected from a database.
// This example complements the one described with serialize().
$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn, "SELECT data FROM sessions WHERE id = ?");
$sqldata = array($_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata) || !odbc_fetch_into($stmt, $tmp)) {
// if the execute or fetch fails, initialize to empty array
$session_data = array();
} else {
// we should now have the serialized data in $tmp[0].
$session_data = unserialize($tmp[0]);
if (!is_array($session_data)) {
// something went wrong, initialize to empty array
$session_data = array();
}
}
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>unserialize_callback_func example</title>
<programlisting role="php">
<![CDATA[
<?php
$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';
ini_set('unserialize_callback_func', 'mycallback'); // set your callback_function
function mycallback($classname)
{
// just include a file containing your class definition
// you get $classname to figure out which class definition is required
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
&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 <parameter>data</parameter> with
<literal>serialize(false)</literal> or by catching the issued
<constant>E_NOTICE</constant>.
</para>
</warning>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>json_encode</function></member>
<member><function>json_decode</function></member>
<member><function>hash_hmac</function></member>
<member><function>serialize</function></member>
<member><link linkend="language.oop5.autoload">Autoloading Classes</link></member>
<member><link linkend="ini.unserialize-callback-func">unserialize_callback_func</link></member>
<member><link linkend="ini.unserialize-max-depth">unserialize_max_depth</link></member>
<member><link linkend="object.wakeup">__wakeup()</link></member>
<member><link linkend="object.serialize">__serialize()</link></member>
<member><link linkend="object.unserialize">__unserialize()</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|