>  기사  >  php教程  >  PHP 使用 session_set_save_handler() 对 Session 进行自定义处理

PHP 使用 session_set_save_handler() 对 Session 进行自定义处理

PHP中文网
PHP中文网원래의
2016-05-25 17:15:581016검색

php代码

/*
Session open (called by session_start( ))
Session close (called at page end)
Session read (called after session_start( ) )
Session write (called when session data is to be written)
Session destroy (called by session_destroy( ) )
Session garbage collect (called randomly)
*/
<?
    function sess_open($sess_path, $sess_name) {
            print "Session opened.\n";
            print "Sess_path: $sess_path\n";
            print "Sess_name: $sess_name\n\n";
            return true;
    }

    function sess_close( ) {
            print "Session closed.\n";
            return true;
    }

    function sess_read($sess_id) {
            print "Session read.\n";
            print "Sess_ID: $sess_id\n";
            return &#39;&#39;;
    }

    function sess_write($sess_id, $data) {
            print "Session value written.\n";
            print "Sess_ID: $sess_id\n";
            print "Data: $data\n\n";
            return true;
    }

    function sess_destroy($sess_id) {
            print "Session destroy called.\n";
            return true;
    }

    function sess_gc($sess_maxlifetime) {
            print "Session garbage collection called.\n";
            print "Sess_maxlifetime: $sess_maxlifetime\n";
            return true;
    }

    session_set_save_handler("sess_open", "sess_close", "sess_read",
                    "sess_write", "sess_destroy", "sess_gc");
    session_start( );

    $_SESSION[&#39;foo&#39;] = "bar";
    print "Some text\n";
    $_SESSION[&#39;baz&#39;] = "wombat";
?>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.