php - Warning: session_set_save_handler() expects parameter 1 to be SessionHandlerInterface, object given -
i've searched solution issue in numerous places no luck. i'm @ complete loss, i'm new modifying sessions , trying implement secure session interface , session handler. error is
warning: session_set_save_handler() expects parameter 1 sessionhandlerinterface, object given in c:\wamp\www\secsessionhandler.php on line 124.
sorry in advance long post!
here 2 respective files:
secsession.php:
<?php class secsession { /** * encryption algorithm */ protected $_algo= mcrypt_rijndael_128; /** * key encryption/decryption */ protected $_key; /** * key hmac authentication */ protected $_auth; /** * path of session file */ protected $_path; /** * session name (optional) */ protected $_name; /** * size of iv vector encryption */ protected $_ivsize; /** * cookie variable name of encryption + auth key */ protected $_keyname; /** * generate random key using openssl * fallback mcrypt_create_iv */ protected function _randomkey($length=128) { if(function_exists('openssl_random_pseudo_bytes')) { $rnd = openssl_random_pseudo_bytes($length, $strong); if ($strong === true) { return $rnd; } } return mcrypt_create_iv($length, mcrypt_dev_urandom); } /** * constructor */ public function __construct() { session_set_save_handler( array($this, "open"), array($this, "close"), array($this, "read"), array($this, "write"), array($this, "destroy"), array($this, "gc") ); if (!extension_loaded('mcrypt')) { throw new exception("the securesession class needs mcrypt php extension, please install it."); } } /** * open session * * @param string $save_path * @param string $session_name * @return bool */ public function open($save_path, $session_name) { $this->_path = $save_path.'/'; $this->_name = $session_name; $this->_keyname = "key_$session_name"; $this->_ivsize = mcrypt_get_iv_size($this->_algo, mcrypt_mode_cbc); if (empty($_cookie[$this->_keyname]) || strpos($_cookie[$this->_keyname],':')===false) { $keylength = mcrypt_get_key_size($this->_algo, mcrypt_mode_cbc); $this->_key = self::_randomkey($keylength); $this->_auth = self::_randomkey(32); $cookie_param = session_get_cookie_params(); setcookie( $this->_keyname, base64_encode($this->_key) . ':' . base64_encode($this->_auth), ($cookie_param['lifetime'] > 0) ? time() + $cookie_param['lifetime'] : 0, $cookie_param['path'], $cookie_param['domain'], $cookie_param['secure'], $cookie_param['httponly'] ); } else { list ($this->_key, $this->_auth) = explode (':',$_cookie[$this->_keyname]); $this->_key = base64_decode($this->_key); $this->_auth = base64_decode($this->_auth); } return true; } /** * close session */ public function close() { return true; } /** * read , decrypt session */ public function read($id) { $sess_file = $this->_path.$this->_name."_$id"; if (!file_exists($sess_file)) { return false; } $data = file_get_contents($sess_file); list($hmac, $iv, $encrypted)= explode(':',$data); $iv = base64_decode($iv); $encrypted = base64_decode($encrypted); $newhmac = hash_hmac('sha256', $iv . $this->_algo . $encrypted, $this->_auth); if ($hmac !== $newhmac) { return false; } $decrypt = mcrypt_decrypt( $this->_algo, $this->_key, $encrypted, mcrypt_mode_cbc, $iv ); return rtrim($decrypt, "\0"); } /** * encrypt , write session */ public function write($id, $data) { $sess_file = $this->_path . $this->_name . "_$id"; $iv = mcrypt_create_iv($this->_ivsize, mcrypt_dev_urandom); $encrypted = mcrypt_encrypt( $this->_algo, $this->_key, $data, mcrypt_mode_cbc, $iv ); $hmac = hash_hmac('sha256', $iv . $this->_algo . $encrypted, $this->_auth); $bytes = file_put_contents($sess_file, $hmac . ':' . base64_encode($iv) . ':' . base64_encode($encrypted)); return ($bytes !== false); } /** * destroy session */ public function destroy($id) { $sess_file = $this->_path . $this->_name . "_$id"; setcookie ($this->_keyname, '', time() - 3600); return(@unlink($sess_file)); } /** * garbage collector */ public function gc($max) { foreach (glob($this->_path . $this->_name . '_*') $filename) { if (filemtime($filename) + $max < time()) { @unlink($filename); } } return true; } } secsessionhandler.php:
<?php class securesessionhandler extends secsession { protected $key, $name, $cookie; public function start() { if (session_id() === '') { if (session_start()) { return mt_rand(0, 4) === 0 ? $this->refresh() : true; // 1/5 } } return false; } public function forget() { if (session_id() === '') { return false; } $_session = []; setcookie( $this->name, '', time() - 42000, $this->cookie['path'], $this->cookie['domain'], $this->cookie['secure'], $this->cookie['httponly'] ); return session_destroy(); } public function refresh() { return session_regenerate_id(true); } public function isexpired($ttl = 30) { $last = isset($_session['_last_activity']) ? $_session['_last_activity'] : false; if ($last !== false && time() - $last > $ttl * 60) { return true; } $_session['_last_activity'] = time(); return false; } public function isfingerprint() { $hash = md5( $_server['http_user_agent'] . (ip2long($_server['remote_addr']) & ip2long('255.255.0.0')) ); if (isset($_session['_fingerprint'])) { return $_session['_fingerprint'] === $hash; } $_session['_fingerprint'] = $hash; return true; } public function isvalid() { return ! $this->isexpired() && $this->isfingerprint(); } public function get($name) { $parsed = explode('.', $name); $result = $_session; while ($parsed) { $next = array_shift($parsed); if (isset($result[$next])) { $result = $result[$next]; } else { return null; } } return $result; } public function put($name, $value) { $parsed = explode('.', $name); $session =& $_session; while (count($parsed) > 1) { $next = array_shift($parsed); if ( ! isset($session[$next]) || ! is_array($session[$next])) { $session[$next] = []; } $session =& $session[$next]; } $session[array_shift($parsed)] = $value; } } $session = new securesessionhandler(); ini_set('session.save_handler', 'files'); session_set_save_handler($session, false); session_save_path(__dir__ . '\sessions'); $session->start(); if ( ! $session->isvalid(5)) { $session->destroy(); } $session->put('xxxx', 'xxxxxx');
you forgot tell php secsession class implements interface sessionhandlerinterface. can in way:
class secsession implements sessionhandlerinterface {
Comments
Post a Comment