Home  >  Article  >  Backend Development  >  Several supplementary functions about session (3)_PHP tutorial

Several supplementary functions about session (3)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:21:58727browse

! ! 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) {


global $sess_save_path, $sess_session_name;
$sess_file = "$sess_save_path/sess_$id";
if ($fp = @fopen( $sess_file, "w")) {
return(fwrite($fp, $sess_data));
} else {
return(false);
}
}
function destroy ($id) {
global $sess_save_path, $sess_session_name;
$sess_file = "$sess_save_path/sess_$id";
return(@unlink($sess_file));
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532386.htmlTechArticle! ! 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. Furthermore, based on my testing, if you want to...
Statement:
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