Article title: analysis of several complementary functions of session. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
In PHP, there are a lot of discussions about Sessions. In fact, there are still several functions in PHP4 that we have not noticed at ordinary times.
Let me introduce them to you.
Session_set_save_handler () is really a good thing.
//********************
Session_unset (PHP4> = 4.0b4)
Void session_unset (void );
This function can leave all session variables registered to null. Note that it is neither unregister nor destroy.
In the following example, this function is well described.
Session_register ('A', 'B', 'C'); // auto-session-start
$ A = 1;
$ B = 2;
$ C = 3;
Session_unregister ('A'); // unregistrered $
Echo "A: $ a-reg:". session_is_registered ('A ')."
"; // But the global $ a remains
Session_unset (); // unsets $ B und $ c
Echo "B: $ B-reg:". session_is_registered ('B ')."
"; // The registration remains!
Echo "C: $ c-reg:". session_is_registered ('C ')."
";
Echo session_encode ();
?>
Output:
A: 1-reg:
B:-reg: 1
C:-reg: 1
! B |! C |
//********************************
Session_get_cookie_params (PHP4> = 4.0RC2)
Array session_get_cookie_params (void );
Returns an array that records the cookie information of the current session.
Include:
"Lifetime"-the lifetime of the cookie.
"Path"-cookie storage path.
"Domain"-cookie domain.
//*******************************
Session_set_cookie_params (PHP4> = 4.0b4)
Void session_set_cookie_params (int lifetime [, string path [, string domain])
This function only applies to the current script file.
//*******************************
The function to be introduced below should be very useful to everyone. are you interested in customizing a session to save without cookies? This function can achieve your assumption.
Let me think about the benefits of using cookies? At least, you don't have to worry about whether the client's cookie function is enabled, right.
Session_set_save_handler (PHP4> = 4.0b4)
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
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.
?>
//************************************** *
Session_cache_limiter (PHP4 CVS only)
String session_cache_limiter ([string cache_limiter])
This function can be used to set or obtain the value of session. cache_limiter.
You can also make the same settings in php. ini. The values include nocache, public, and private.
This function is sent to the client through the HTTP header. If it is nocache, the cache of any client is prohibited. Public will allow cache, but private is more secure than public.
The default value is set in php. ini. if you want to use it, you must call this function before calling session_start.
This function can only be run in CVS mode, but PHP4.0.3 will support it.
Example 1. session_cache_limiter () examples
# Set the cache limiter to 'private'
Session_cache_limiter ('private );
$ Cache_limiter = session_cache_limiter ();
Echo "The cache limiter is now set to $ cache_limiter
";
?>
//*****************************