Home >Backend Development >PHP Tutorial >Several supplementary functions about session (3)_PHP tutorial
! ! Note: Before using this function, you must first configure the php.ini file, session.save_hadler=user, otherwise, session_set_save_handler() will not take effect.
In addition, according to my tests, if you want such a session to be used across pages, you must add your own custom function and session_set_save_handler to each script file that uses the session. Therefore, the best way is to do into a separate file and include it in every script that uses session.
The following example provides a basic session saving method, similar to the default files method.
If you want to use a database, this is also easy to do.
Example 1. session_set_save_handler() example
$#@60;?php
function open ($save_path, $session_name) {
global $sess_save_path, $sess_session_name;
$sess_save_path = $ save_path;
$sess_session_name = $session_name;
return(true);
}
function close() {
return(true);
}
function read ($ id) {
global $sess_save_path, $sess_session_name;
$sess_file = "$sess_save_path/sess_$id";
if ($fp = @fopen($sess_file, "r")) {
$sess_data = fread($fp, filesize($sess_file));
return($sess_data);
} else {
return("");
}
}
function write ($id, $sess_data) {