3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function setKey($session_keys, $value) { $current_key = ''; $counter = 0; $prev_step = $_SESSION; try { $session_keys_array = explode(':', $session_keys); foreach ($session_keys_array as $key) { $counter++; $current_key .= '[\'' . $key . '\']'; if (count($session_keys_array) > $counter) { if (is_array($prev_step) && !array_key_exists($key, $prev_step)) { eval('$_SESSION' . $current_key . ' = array();'); } else if (!is_array($prev_step) || (array_key_exists($key, $prev_step)) && !is_array($prev_step[$key]) ) { throw new Exception ("\$_SESSION$current_key is not an array"); } eval('$prevStep = $_SESSION' . $current_key . ';'); } else { eval('$_SESSION' . $current_key . ' = ' . $value . ';'); } } } catch (Exception $e) { //TODO:log notice if Session[currentKey] is not an array echo $e->getMessage(); return false; } return eval('!empty($_SESSION' . $current_key . ');'); } function setSessionTestingData() { $agent = 5; $ticket = 6; $_SESSION = array( 'agent_id' => '2', 'ticket_id' => '3', 'agent' => $agent, 'ticket' => $ticket, 'sub_array' => array( 'sub' => 4, 'agent' => $agent, 'ticket' => $ticket, ), ); return $_SESSION; } $_SESSION = setSessionTestingData(); var_dump(SessionManager::setKey('sub_array:sub:unexistant:none', '4'));
based on jdCvS
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Class 'SessionManager' not found in /in/Qu80h:59 Stack trace: #0 {main} thrown in /in/Qu80h on line 59
Process exited with code 255.
Output for 5.3.21 - 5.4.20, 5.4.22 - 5.6.28
Fatal error: Class 'SessionManager' not found in /in/Qu80h on line 59
Process exited with code 255.
Output for 5.4.21

Process exited with code 137.