session_module_name

(PHP 4, PHP 5, PHP 7, PHP 8)

session_module_name获取/设置会话模块名称

说明

session_module_name ( string $module = ? ) : string

session_module_name() 获取或设置会话模块名称,也被称做:session.save_handler

参数

module

如果指定 module 参数,则使用 指定值作为会话模块。 禁止传入 "user" 作为此参数的值, 请使用 set_set_save_handler() 来设置用户自定义的会话处理器。

返回值

返回当前所用的会话模块名称。

更新日志

版本 说明
7.2.0 不允许设置模块名称为 "user"。 在之前的版本中,如果设置为 "user",那么会被静默的忽略到。

User Contributed Notes

raees at steelbrain dot com dot pk 03-Nov-2014 09:29
This function is used to set the Session Module at site or script level.

The global configuration can be done in php.ini under the [Session] section and with the name of "session.save_handler". The sessions are saved in files by default, like so:
session.save_handler = files

But with this configuration you set one of your websites to use some other session module (if you have them installed and extension loaded with PHP), like so:
<?php

// NOTE: You must use this function before starting session with session_start(); to make it work properly
session_module_name('memcache'); // or pgsql or redis etc

// You'll need to define a save path also, if the module is other than files, like so:
session_save_path('localhost:11211'); // memcache uses port 11211

// or you can use multiple for load balancing:
session_save_path('localhost:11211:41,otherhost:11211:60') // First part is hostname or path to socket, next is port and the last is the weight for that server

//The function also returns the value of the current session module.
echo session_module_name(); // will print memcache in our case

// or maybe a check
if(session_module_name() != 'memcache'){
 
// Do something, throw an exception maybe
}