<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.var.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.unserialize.php',
    1 => 'unserialize',
    2 => 'Creates a PHP value from a stored representation',
  ),
  'up' => 
  array (
    0 => 'ref.var.php',
    1 => 'Variable handling Functions',
  ),
  'prev' => 
  array (
    0 => 'function.strval.php',
    1 => 'strval',
  ),
  'next' => 
  array (
    0 => 'function.unset.php',
    1 => 'unset',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/var/functions/unserialize.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.unserialize" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">unserialize</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">unserialize</span> &mdash; <span class="dc-title">
   Creates a PHP value from a stored representation
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.unserialize-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>unserialize</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$data</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</span></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="simpara">
   <span class="function"><strong>unserialize()</strong></span> takes a single serialized variable and
   converts it back into a PHP value.
  </p>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    Do not pass untrusted user input to <span class="function"><strong>unserialize()</strong></span> regardless
    of the <code class="parameter">options</code> value of <code class="literal">allowed_classes</code>.
    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
    <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> and <span class="function"><a href="function.json-encode.php" class="function">json_encode()</a></span>) if
    you need to pass serialized data to the user.
   </p>
   <p class="para">
    If you need to unserialize externally-stored serialized data, consider using
    <span class="function"><a href="function.hash-hmac.php" class="function">hash_hmac()</a></span> for data validation. Make sure data is
    not modified by anyone but you.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.unserialize-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       The serialized string.
      </p>
      <p class="para">
       If the variable being unserialized is an object, after successfully
       reconstructing the object PHP will automatically attempt to call the
       <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a> or <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a> methods (if one exists).
      </p>
      <p class="para">
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <strong>
         <a href="var.configuration.php#ini.unserialize-callback-func" class="link">unserialize_callback_func</a>
          directive
        </strong><br />
        <p class="para">
         The callback specified in the
         <a href="var.configuration.php#ini.unserialize-callback-func" class="link">unserialize_callback_func</a>
         directive is called when an undefined class is unserialized.
         If no callback is specified, the object will be instantiated as
         <span class="classname"><a href="class.php-incomplete-class.php" class="classname">__PHP_Incomplete_Class</a></span>.
        </p>
       </p></blockquote>
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       Any options to be provided to <span class="function"><strong>unserialize()</strong></span>, as an
       associative array.
      </p>
      <table class="doctable table">
       <caption><strong>Valid options</strong></caption>
       
        <thead>
         <tr>
          <th>Name</th>
          <th>Type</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><code class="literal">allowed_classes</code></td>
          <td><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></td>
          <td>
           <span class="simpara">
            Either an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of class names which should be
            accepted, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> to accept no classes, or <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> to accept all
            classes. If this option is defined and
            <span class="function"><strong>unserialize()</strong></span> encounters an object of a class
            that isn&#039;t to be accepted, then the object will be instantiated as
            <span class="classname"><a href="class.php-incomplete-class.php" class="classname">__PHP_Incomplete_Class</a></span> instead.
           </span>
           <span class="simpara">
            Omitting this option is the same as defining it as <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>: PHP
            will attempt to instantiate objects of any class.
           </span>
          </td>
         </tr>

         <tr>
          <td><code class="literal">max_depth</code></td>
          <td><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></td>
          <td>
           <span class="simpara">
            The maximum depth of structures permitted during unserialization,
            and is intended to prevent stack overflows. The default depth limit
            is <code class="literal">4096</code> and can be disabled by setting
            <code class="literal">max_depth</code> to <code class="literal">0</code>.
           </span>
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.unserialize-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The converted value is returned, and can be a <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>,
   <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, <span class="type"><a href="language.types.float.php" class="type float">float</a></span>, <span class="type"><a href="language.types.string.php" class="type string">string</a></span>,
   <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or <span class="type"><a href="language.types.object.php" class="type object">object</a></span>.
  </p>
  <p class="para">
   In case the passed string is not unserializeable, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is returned and
   <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is issued.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.unserialize-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="simpara">
   Objects may throw <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span>s in their unserialization handlers.
  </p>
  <p class="simpara">
   As of PHP 8.4.0, if the <code class="literal">allowed_classes</code> element of
   <code class="parameter">options</code> is not an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of class names,
   <span class="function"><strong>unserialize()</strong></span> throws <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span>s
   and <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>s.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.unserialize-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.4.0</td>
       <td>
        Now throws <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span>s and
        <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>s if the <code class="literal">allowed_classes</code>
        element of <code class="parameter">options</code> is not an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of class names.
       </td>
      </tr>

      <tr>
       <td>8.3.0</td>
       <td>
        Now emits <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> when the input string has unconsumed data.
       </td>
      </tr>

      <tr>
       <td>8.3.0</td>
       <td>
        Now emits <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> when the passed string is not unserializeable;
        previously <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> was emitted.
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        Added the <code class="literal">max_depth</code> element of
        <code class="parameter">options</code> to set the maximum depth of structures permitted during unserialization.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        The <code class="literal">allowed_classes</code> element of
        <code class="parameter">options</code> is now strictly typed, i.e. if anything
        other than an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or a <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> is given,
        <span class="function"><strong>unserialize()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> and issues an
        <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.unserialize-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>unserialize()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Here, we use unserialize() to load session data to the<br />// $session_data array from the string selected from a database.<br />// This example complements the one described with serialize().<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">odbc_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"webdb"</span><span style="color: #007700">, </span><span style="color: #DD0000">"php"</span><span style="color: #007700">, </span><span style="color: #DD0000">"chicken"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">odbc_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT data FROM sessions WHERE id = ?"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sqldata </span><span style="color: #007700">= array(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]);<br />if (!</span><span style="color: #0000BB">odbc_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">$sqldata</span><span style="color: #007700">) || !</span><span style="color: #0000BB">odbc_fetch_into</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">, </span><span style="color: #0000BB">$tmp</span><span style="color: #007700">)) {<br />    </span><span style="color: #FF8000">// if the execute or fetch fails, initialize to empty array<br />    </span><span style="color: #0000BB">$session_data </span><span style="color: #007700">= array();<br />} else {<br />    </span><span style="color: #FF8000">// we should now have the serialized data in $tmp[0].<br />    </span><span style="color: #0000BB">$session_data </span><span style="color: #007700">= </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmp</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />    if (!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$session_data</span><span style="color: #007700">)) {<br />        </span><span style="color: #FF8000">// something went wrong, initialize to empty array<br />        </span><span style="color: #0000BB">$session_data </span><span style="color: #007700">= array();<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 unserialize_callback_func example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$serialized_object</span><span style="color: #007700">=</span><span style="color: #DD0000">'O:1:"a":1:{s:5:"value";s:3:"100";}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'unserialize_callback_func'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mycallback'</span><span style="color: #007700">); </span><span style="color: #FF8000">// set your callback_function<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">mycallback</span><span style="color: #007700">(</span><span style="color: #0000BB">$classname</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #FF8000">// just include a file containing your class definition<br />    // you get $classname to figure out which class definition is required<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$classname</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$serialized_object</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.unserialize-notes">
  <h3 class="title">Notes</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is returned both in the case of an error and if unserializing
    the serialized <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> value. It is possible to catch this special case by
    comparing <code class="parameter">data</code> with
    <code class="literal">serialize(false)</code> or by catching the issued
    <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.unserialize-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.json-encode.php" class="function" rel="rdfs-seeAlso">json_encode()</a> - Returns the JSON representation of a value</span></li>
    <li><span class="function"><a href="function.json-decode.php" class="function" rel="rdfs-seeAlso">json_decode()</a> - Decodes a JSON string</span></li>
    <li><span class="function"><a href="function.hash-hmac.php" class="function" rel="rdfs-seeAlso">hash_hmac()</a> - Generate a keyed hash value using the HMAC method</span></li>
    <li><span class="function"><a href="function.serialize.php" class="function" rel="rdfs-seeAlso">serialize()</a> - Generates a storable representation of a value</span></li>
    <li><a href="language.oop5.autoload.php" class="link">Autoloading Classes</a></li>
    <li><a href="var.configuration.php#ini.unserialize-callback-func" class="link">unserialize_callback_func</a></li>
    <li><a href="var.configuration.php#ini.unserialize-max-depth" class="link">unserialize_max_depth</a></li>
    <li><a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a></li>
    <li><a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a></li>
    <li><a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>