From http://blog.opendigest.org/show-98-1.html
Void session_set_save_handler (string open, string close, string read, string write, string destroy, string GC)
This function can define user-level session storage functions (Open, close, write, etc ).
For example, this function is useful when we want to save the session in a local database.
Note: before using this function, configure the php. ini file and session. save_hadler = user. Otherwise, session_set_save_handler () will not take effect.
In addition, according to my tests, if you want this session to be used across pages, you must add your custom function and session_set_save_handler to each session script file. Therefore, the best way is to make a separate file and include it in every session script.
The following example provides a basic session persistence method, similar to the default files method.
If you want to use a database, this is also easy to implement.
Example 1. session_set_save_handler () Example
<? 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 ));
}
/*************************************** ******
* Warning-you will need to implement some *
* Sort of garbage collection routine here .*
**************************************** *****/
Function GC ($ maxlifetime ){
Return true;
}
Session_set_save_handler ("open", "close", "read", "write", "Destroy", "GC ");
Session_start ();
// Proceed to use sessions normally
// Now you can use the session as usual.
?>