Annotation of embedaddon/php/ext/session/tests/session_set_save_handler_class_002.phpt, revision 1.1.1.2

1.1       misho       1: --TEST--
                      2: Test session_set_save_handler() : full handler implementation
                      3: --INI--
                      4: session.save_handler=files
                      5: session.name=PHPSESSID
                      6: --SKIPIF--
                      7: <?php include('skipif.inc'); ?>
                      8: --FILE--
                      9: <?php
                     10: 
                     11: ob_start();
                     12: 
                     13: /* 
                     14:  * Prototype : bool session_set_save_handler(SessionHandler $handler [, bool $register_shutdown_function = true])
                     15:  * Description : Sets user-level session storage functions
                     16:  * Source code : ext/session/session.c 
                     17:  */
                     18: 
                     19: echo "*** Testing session_set_save_handler() : full handler implementation ***\n";
                     20: 
                     21: class MySession2 extends SessionHandler {
                     22:        public $path;
                     23: 
                     24:        public function open($path, $name) {
                     25:                if (!$path) {
1.1.1.2 ! misho      26:                        $path = sys_get_temp_dir();
1.1       misho      27:                }
                     28:                $this->path = $path . '/u_sess_' . $name;
                     29:                return true;
                     30:        }
                     31: 
                     32:        public function close() {
                     33:                return true;
                     34:        }
                     35: 
                     36:        public function read($id) {
                     37:                return @file_get_contents($this->path . $id);
                     38:        }
                     39: 
                     40:        public function write($id, $data) {
                     41:                return file_put_contents($this->path . $id, $data);
                     42:        }
                     43: 
                     44:        public function destroy($id) {
                     45:                @unlink($this->path . $id);
                     46:        }
                     47: 
                     48:        public function gc($maxlifetime) {
                     49:                foreach (glob($this->path . '*') as $filename) {
                     50:                        if (filemtime($filename) + $maxlifetime < time()) {
                     51:                                @unlink($filename);
                     52:                        }
                     53:                }
                     54:                return true;
                     55:        }
                     56: }
                     57: 
                     58: $handler = new MySession2;
                     59: session_set_save_handler(array($handler, 'open'), array($handler, 'close'),
                     60:        array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc'));
                     61: session_start();
                     62: 
                     63: $_SESSION['foo'] = "hello";
                     64: 
                     65: var_dump(session_id(), ini_get('session.save_handler'), $_SESSION);
                     66: 
                     67: session_write_close();
                     68: session_unset();
                     69: 
                     70: session_start();
                     71: var_dump($_SESSION);
                     72: 
                     73: session_write_close();
                     74: session_unset();
                     75: 
                     76: session_set_save_handler($handler);
                     77: session_start();
                     78: 
                     79: $_SESSION['foo'] = "hello";
                     80: 
                     81: var_dump(session_id(), ini_get('session.save_handler'), $_SESSION);
                     82: 
                     83: session_write_close();
                     84: session_unset();
                     85: 
                     86: session_start();
                     87: var_dump($_SESSION);
                     88: 
                     89: session_write_close();
                     90: session_unset();
                     91: 
                     92: --EXPECTF--
                     93: *** Testing session_set_save_handler() : full handler implementation ***
                     94: string(%d) "%s"
                     95: string(4) "user"
                     96: array(1) {
                     97:   ["foo"]=>
                     98:   string(5) "hello"
                     99: }
                    100: array(1) {
                    101:   ["foo"]=>
                    102:   string(5) "hello"
                    103: }
                    104: string(%d) "%s"
                    105: string(4) "user"
                    106: array(1) {
                    107:   ["foo"]=>
                    108:   string(5) "hello"
                    109: }
                    110: array(1) {
                    111:   ["foo"]=>
                    112:   string(5) "hello"
                    113: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>