Current Location: Home> Function Categories> SessionHandlerInterface::gc

SessionHandlerInterface::gc

Cleanup old sessions
Name:SessionHandlerInterface::gc
Category:Session Session
Programming Language:php
One-line Description:Called during session garbage collection

Function name: SessionHandlerInterface::gc()

Applicable version: PHP 5 >= 5.4.0, PHP 7

Function Description: This function is called during session garbage collection. Its purpose is to clear out expired session data.

usage:

  1. First, you need to implement a class to implement the SessionHandlerInterface interface, for example:
 class MySessionHandler implements SessionHandlerInterface { public function open($savePath, $sessionName) { // 在这里打开会话存储return true; } public function close() { // 在这里关闭会话存储return true; } public function read($sessionId) { // 在这里读取会话数据return ''; } public function write($sessionId, $data) { // 在这里写入会话数据return true; } public function destroy($sessionId) { // 在这里销毁会话数据return true; } public function gc($maxlifetime) { // 在这里执行会话垃圾回收return true; } }
  1. Then you need to register the class as a session handler:
 $handler = new MySessionHandler(); session_set_save_handler($handler, true);
  1. Finally, you can call the gc() function in the following ways:
 $handler->gc($maxlifetime);

Example:

 class MySessionHandler implements SessionHandlerInterface { // ... public function gc($maxlifetime) { // 清除超过$maxlifetime秒的会话数据$expiredTime = time() - $maxlifetime; // 执行清除操作的代码return true; } } $handler = new MySessionHandler(); session_set_save_handler($handler, true); // 调用gc()函数进行会话垃圾回收$handler->gc(ini_get('session.gc_maxlifetime'));

The above example shows how to implement the gc() method of the SessionHandlerInterface interface and use the gc() function for session garbage collection in a custom session handler. In the example, the gc() function is used to clear session data that exceeds the specified maximum life cycle.

Similar Functions
Popular Articles