session_set_save_handler

http://topic.csdn.net/u/20081127/14/23222d65-0ac1-4446-b898-69bda2fb901f.html


        session_module_name('user'); //session文件保存方式,这个是必须的!除非在Php.ini文件中设置了
        session_set_save_handler( 
            array(&$this, 'open'), //在运行session_start()时执行

            array(&$this, 'close'), //在脚本执行完成或调用session_write_close() 或 session_destroy()时被执行,即在所有session操作完后被执行

            array(&$this, 'read'), //在运行session_start()时执行,因为在session_start时,会去read当前session数据
            array(&$this, 'write'), //此方法在脚本结束和使用session_write_close()强制提交SESSION数据时执行

            array(&$this, 'destroy'), //在运行session_destroy()时执行

            array(&$this, 'gc') //执行概率由session.gc_probability 和 session.gc_divisor的值决定,时机是在open,read之后,session_start会相继执行open,read和gc
        ); 
        session_start(); //这也是必须的,打开session,必须在session_set_save_handler后面执行



************************************************************************************************************************************************************************************

http://cn.php.net/manual/en/function.session-set-save-handler.php


session_set_save_handler Sets user-level session storage functions

reject note Description

bool session_set_save_handler ( callback$open , callback$close , callback$read , callback$write , callback$destroy , callback$gc )

Since PHP 5.4 it is possible to register the following prototype:

bool session_set_save_handler ( SessionHandlerInterface$sessionhandler [, bool$register_shutdown = true ] )

session_set_save_handler() sets the user-level session storage functions which are used for storing and retrieving data associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred. i.e. Storing the session data in a local database.


This function has two prototypes.

sessionhandler

An instance of a class implementing SessionHandlerInterface, such asSessionHandler, to register as the session handler. Since PHP 5.4 only.

register_shutdown

Register session_register_shutdown() as aregister_shutdown_function() function.

or
open(string $savePath, string $sessionName)

The open callback works like a constructor in classes and is executed when the session is being opened. It is the first callback function executed when the session is started automatically or manually withsession_start(). Return value isTRUE for success, FALSE for failure.

close()

The close callback works like a destructor in classes and is executed after the session write callback has been called. It is also invoked whensession_write_close() is called. Return value should beTRUE for success, FALSE for failure.

read(string $sessionId)

The read callback must always return a session encoded (serialized) string, or an empty string if there is no data to read.

This callback is called internally by PHP when the session starts or whensession_start() is called. Before this callback is invoked PHP will invoke theopen callback.

The value this callback returns must be in exactly the same serialized format that was originally passed for storage to thewrite callback. The value returned will be unserialized automatically by PHP and used to populate the $_SESSION superglobal. While the data looks similar toserialize() please note it is a different format which is speficied in thesession.serialize_handler ini setting.

write(string $sessionId, string $data)

The write callback is called when the session needs to be saved and closed. This callback receives the current session ID a serialized version the $_SESSION superglobal. The serialization method used internally by PHP is specified in the session.serialize_handler ini setting.

The serialized session data passed to this callback should be stored against the passed session ID. When retrieving this data, theread callback must return the exact value that was originally passed to thewrite callback.

This callback is invoked when PHP shuts down or explicitly when session_write_close() is called. Note that after executing this function PHP will internally execute theclose callback.

Note:

The "write" handler is not executed until after the output stream is closed. Thus, output from debugging statements in the "write" handler will never be seen in the browser. If debugging output is necessary, it is suggested that the debug output be written to a file instead.

destroy($sessionId)

This callback is executed when a session is destroyed with session_destroy() or withsession_regenerate_id() with the destroy parameter set toTRUE. Return value should be TRUE for success,FALSE for failure.

gc($lifetime)

The garbage collector callback is invoked internally by PHP periodically in order to purge old session data. The frequency is controlled bysession.gc_probability and session.gc_divisor. The value of lifetime which is passed to this callback can be set insession.gc_maxlifetime. Return value should be TRUE for success,FALSE for failure.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值