Note 017 session_set_save_handler() function in PHP

黄舟
Release: 2023-03-04 09:20:01
Original
1612 people have browsed it

Function description

session_set_save_handler function is used to set user-defined session storage.

If you want to use a method other than PHP's built-in session storage mechanism, you can use this function. For example, you can customize the session storage function to store session data to a database.

Usage

bool session_set_save_handler ( callable $open , callable $close , callable $read , callable $write , callable $destroy , callable $gc [, callable $create_sid ] )

Starting from PHP 5.4 version, you can directly use simpler parameters to customize session storage settings:

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

parameters Note

This parameter has two prototypes:

The first prototype (available for PHP 5.4+ version) is:

sessionhandler

implements the SessionHandlerInterface interface Object, you can customize this object, or you can use the officially provided SessionHandler

register_shundown

to register the function session_write_close() as the register_shutdown_function() function.

The second prototype is:

open(string $savePath, string $sessionName)

open The callback function is similar to the constructor of the class. When the session is opened, it will is called. This is the first callback function called after starting a session automatically or manually by calling session_start(). This callback function returns true if the operation is successful, otherwise it returns false.

close()

close The callback function is similar to the destructor of a class. Called after the write callback function is called. When the session_write_close() function is called, the close callback function will also be called. This callback function returns true if the operation is successful, otherwise it returns false.

read(string $sessionId)

If there is data in the session, the read callback function must return a string that encodes (serializes) the session data. If there is no data in the session, the read callback function returns an empty string.

After starting the session automatically or manually by calling the session_start() function, PHP internally calls the read callback function to obtain the session data. Before calling read, PHP will call the open callback function.

The serialized string format returned by the read callback must be exactly the same as the format when the write callback function saves the data. PHP will automatically deserialize the returned string and populate the $_SESSION super global variable. Although the data looks very similar to the serialize() function, it is important to remember that they are different.

write(string $sessionId, string $data)

The write callback function will be called when the session saves data. This callback function receives the current session ID and the string after serialization of the data in $_SESSION as parameters. The process of serializing session data is completed by PHP according to the session.serialize_handler setting value.

The serialized data will be saved in association with the session ID. When calling the read callback function to obtain data, the returned data must be completely consistent with the data passed into the write callback function.

PHP will call this callback function after the script is executed or the session_write_close() function is called. Note that after calling this callback function, PHP will call the close callback function internally.

Note:

PHP will not call the write callback function until the output stream is written and closed, so the debugging information in the write callback function will not be output to the browser. If you need to use debug output in the write callback function, it is recommended to write the debug output to a file.

destroy($sessionId)

This callback function will be called when the session_destroy() function is called, or the session_regenerate_id() function is called and the destroy parameter is set to true. This callback function returns true if the operation is successful, otherwise it returns false.

gc($lifetime)

In order to clean up old data in the session, PHP will call the garbage collection callback function from time to time. The calling cycle is controlled by the session.gc_probability and session.gc_divisor parameters. The lifetime parameter passed into this callback function is set by session.gc_maxlifetime. This callback function returns true if the operation is successful, otherwise it returns false.

create_sid()

Callback function called when a new session ID is required. The callback function is called with no parameters passed in, and its return value should be a valid session ID in string format.

Return Value

Returns true on success, or false on failure.

The above is the content of the session_set_save_handler() function in Note 017 PHP. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!