note 119851 added to function.unserialize

From: Date: Tue, 06 Sep 2016 21:52:59 +0000
Subject: note 119851 added to function.unserialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-206510@lists.php.net to get a copy of this message
You can use the following code to use the php 7 unserialize function in php 5.3 and upwards. This adds the $option argument. <?php namespace { /** * PHP 7 unserialize function for PHP 5.3 upwards. * Added the $option argument (allowed_classes). * See php unserialize manual for more detail. **/ function php7_unserialize($str, $options = array()) { if(version_compare(PHP_VERSION, '7.0.0', '>=')) { return unserialize($str, $options); } $allowed_classes = isset($options['allowed_classes']) ? $options['allowed_classes'] : true; if(is_array($allowed_classes) || !$allowed_classes) { $str = preg_replace_callback( '/(?=^|:)(O|C):\d+:"([^"]*)":(\d+):{/', function($matches) use ($allowed_classes) { if(is_array($allowed_classes) && in_array($matches[2], $allowed_classes)) { return $matches[0]; } else { return $matches[1].':22:"__PHP_Incomplete_Class":'. ($matches[3] + 1). ':{s:27:"__PHP_Incomplete_Class_Name";'. serialize($matches[2]); } }, $str ); } unset($allowed_classes); return unserialize($str); } } // namespace namespace my_name_space { /** * Use the new php7 unserialize in your namespace without * renaming all unserialize(...) function calls to * php7_unserialize(...). **/ function unserialize($str, $options = array()) { return php7_unserialize($str, $options); } } ?> ---- Server IP: 83.137.20.135 Probable Submitter: 141.105.0.254 ---- Manual Page -- http://php.net/manual/en/function.unserialize.php Edit -- https://master.php.net/note/edit/119851 Del: integrated -- https://master.php.net/note/delete/119851/integrated Del: useless -- https://master.php.net/note/delete/119851/useless Del: bad code -- https://master.php.net/note/delete/119851/bad+code Del: spam -- https://master.php.net/note/delete/119851/spam Del: non-english -- https://master.php.net/note/delete/119851/non-english Del: in docs -- https://master.php.net/note/delete/119851/in+docs Del: other reasons-- https://master.php.net/note/delete/119851 Reject -- https://master.php.net/note/reject/119851 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#206510) next »