Welcome Guest, Not a member yet? Register   Sign In
Sessions _serialize not working recursively
#1

[eluser]Unknown[/eluser]
Hey guys !

I was banging my head into the wall this morning because i couldn't figure out what was the problem.

I have a huge session array (i know, it's bad but I REALLY can't do anything else) with some arrays in it.

This is a dumbed down version of what I have :

Code:
$user = array('name' => 'name',
              'downloads' => array('url' => 'http://www.perdu.com', 'name' => 'something\with\blackslashes')
         );

The problem was that when serializing the array, the part with the \es wasn't processed and thus causing the _unserialize method to crash. Of course i do addslashes to my datas.

So i changed the code a little to add some recusivity in it :

Code:
function _serialize($data)
{
  if (is_array($data))
  {
  
   //EDITED PART
   array_walk_recursive($data, function(&$item,$key){
    if (is_string($item))
    {
     $item = str_replace('\\', '{{slash}}', $item);
    }
   });
  }
  else
  {
   if (is_string($data))
   {
    $data = str_replace('\\', '{{slash}}', $data);
   }
  }

  return serialize($data);
}

Now everything is working correctly.

Going through all the session process (database, serialize, unserialze, test for special characters...) was a real pain.


Messages In This Thread
Sessions _serialize not working recursively - by El Forum - 11-22-2011, 06:40 AM
Sessions _serialize not working recursively - by El Forum - 07-02-2012, 01:51 PM
Sessions _serialize not working recursively - by El Forum - 10-17-2013, 05:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB