3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Session Wrapper Object * * Provides abstract support for session handling * * @author Harold Asbridge * @package Sphere * @subpackage Core * @version 3.0 * */ class Session { /** * The current user's session id * * @var string */ public $id; /** * Whether or not the session is open * * @var bool */ public $open = false; /** * Flash data identifier * * @var string */ public $flashdata_key = 'flash'; /** * Flash data * * @var array */ public $userdata = array(); /** * Constructor method - init session * */ public function __construct() { $sesDir = '/var/lib/php/session'; // Set session save path to app-relative dir, if it exists //if (file_exists(BASE_DIR.'/sessions')) if (file_exists($sesDir)) { //if (is_writable(BASE_DIR.'/sessions')) if (is_writable($sesDir)) { //session_save_path(BASE_DIR.'/sessions'); session_save_path($sesDir); } } // Set session cookie to the base href, useful for multiple sites on the same domain if (BASE_HREF != "") { session_set_cookie_params(0, BASE_HREF); } else { session_set_cookie_params(0); } // Begin session session_start(); $this->id = session_id(); $this->open = true; // Initalize Flash Data $this->userdata = ($this->Get('flash_userdata')) ? $this->Get('flash_userdata') : array(); // Delete 'old' flashdata (from last request) $this->_flashdata_sweep(); // Mark all new flashdata as old (data will be deleted before next request) $this->_flashdata_mark(); } /** * Get session value * * @param string $key * @return mixed|null */ public function Get($key) { if (isset($_SESSION[$key])) { return $_SESSION[$key]; } return null; } /** * Set session value * * @param string $key * @param mixed $value * @return bool */ public function Set($key, $value) { $_SESSION[$key] = $value; return true; } /** * Unset a session value * * @param string $key */ public function Clear($key) { if (isset($_SESSION[$key])) { unset($_SESSION[$key]); return true; } return false; } /** * Empty all session vars * * @return bool */ public function Destroy() { $_SESSION = array(); return true; } /** * Close session * * @return bool */ public function Close() { if ($this->open) { session_write_close(); $this->open = false; return true; } return false; } /** * Sets flash data * * @access plublic * @param mixed * @param mixed * @return void */ public function set_flashdata($newdata = array(), $newval = '') { if (is_string($newdata)) { $newdata = array($newdata => $newval); } if (count($newdata) > 0) { foreach ($newdata as $key => $val) { $flashdata_key = $this->flashdata_key.':new:'.$key; $this->set_userdata($flashdata_key, $val); } } } /** * Sets old flash data to new so it will last another page load * * @access plublic * @param string * @return void */ public function keep_flashdata($key) { // 'old' flashdata gets removed. Here we mark all // flashdata as 'new' to preserve it from _flashdata_sweep() // Note the function will return FALSE if the $key // provided cannot be found $old_flashdata_key = $this->flashdata_key.':old:'.$key; $value = $this->userdata($old_flashdata_key); $new_flashdata_key = $this->flashdata_key.':new:'.$key; $this->set_userdata($new_flashdata_key, $value); } /** * Retrieves flash data * * @access plublic * @param string * @return mixed */ public function flashdata($key) { $flashdata_key = $this->flashdata_key.':old:'.$key; return $this->userdata($flashdata_key); } /** * Identifies flashdata as 'old' for removal * when _flashdata_sweep() runs. * * @access private * @return void */ private function _flashdata_mark() { $userdata = $this->all_userdata(); foreach ($userdata as $name => $value) { $parts = explode(':new:', $name); if (is_array($parts) && count($parts) === 2) { $new_name = $this->flashdata_key.':old:'.$parts[1]; $this->set_userdata($new_name, $value); $this->unset_userdata($name); } } } /** * Removes all flashdata marked as 'old' * * @access private * @return void */ private function _flashdata_sweep() { $userdata = $this->all_userdata(); foreach ($userdata as $key => $value) { if (strpos($key, ':old:')) { $this->unset_userdata($key); } } } /** * Fetch a specific item from the session array * * @access public * @param string * @return string */ public function userdata($item) { return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item]; } /** * Add or change data in the "userdata" array * * @access public * @param mixed * @param string * @return void */ public function set_userdata($newdata = array(), $newval = '') { if (is_string($newdata)) { $newdata = array($newdata => $newval); } if (count($newdata) > 0) { foreach ($newdata as $key => $val) { $this->userdata[$key] = $val; } } $this->Set('flash_userdata', $this->userdata); } /** * Delete a session variable from the "userdata" array * * @access array * @return void */ public function unset_userdata($newdata = array()) { if (is_string($newdata)) { $newdata = array($newdata => ''); } if (count($newdata) > 0) { foreach ($newdata as $key => $val) { unset($this->userdata[$key]); } } $this->Set('flash_userdata', $this->userdata); } /** * Fetch a specific item from the session array * * @access public * @return array */ public function all_userdata() { return ( ! isset($this->userdata)) ? FALSE : $this->userdata; } /** * Destructor method - save all session data before exiting * */ public function __destruct() { $this->Close(); } } ?>
Output for 5.0.0 - 7.2.0
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/C3BLR on line 20
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/C3BLR on line 20
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/C3BLR on line 20
Process exited with code 255.