Session_set_save_handler: is the number of sessions generated by this function valid only once? Solution

Source: Internet
Author: User
Session_set_save_handler: is the number of sessions generated by this function valid only once? I used session_set_save_handler to store session data. I found that the generated session is valid only once and the page is refreshed again. other normal assignment sessions are normal. how can I prevent the generated session from disappearing? ------ Solution ----------------- session_set_save_handler: is the number of sessions generated by this function valid only once?
I use session_set_save_handler to store session data.
The generated session is valid only once.
Refresh the page and the page disappears.
Other normal sessions are normal.
Is there any way to prevent the session generated in this way from disappearing?

------ Solution --------------------
Hope to help you

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.


?>
------ Solution --------------------
At least view the code snippet
------ Solution --------------------
What is the validity period of the session in php. ini?
Session. cache_expire = 180

Does your page contain session_destroy (), but session_start () is not displayed ()?
------ Solution --------------------
"Other normal assignment sessions are normal." What does this mean? are there many ways to assign values to your sessions?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.