PHP custom session storage path and deletion, logout, and writing methods

Source: Internet
Author: User
This article mainly introduces PHP's custom session storage path and deletion, logout, and writing methods, and describes common session operation skills in the form of instances, PHP project development has good reference value. For more information, see the examples in this article to describe the PHP custom session storage path and deletion, logout, and write methods. Share it with you for your reference. The specific method is as follows:

The code is as follows:


$ Sessionpath = session_save_path (); // Obtain the save path of the current session
Echo $ sessionpath;
If (strpos ($ sessionpath ,";")! = False) // if a Semicolon exists in the path
{
$ Sessionpath = substr ($ sessionpath, strpos ($ sessionpath, ";") + 1); // set a new path
}
Function open ($ save_path, $ session_name) // defines the function to open.
{
Global $ sess_save_path, $ sess_session_name; // predefined session path and name
$ Sess_save_path = $ save_path; // defines the save path.
$ Sess_session_name = $ session_name; // defines the session name.
Return (true); // returns the true value.
}
Function close () // define the function to close
{
Return (true); // returns the true value directly.
}
Function read ($ id) // defines the read function
{
Global $ sess_save_path, $ sess_session_name; // predefined save path and name
$ Sess_file = "$ sess_save_path/sess _ $ id"; // defines the file
If ($ fp = @ fopen ($ sess_file, "r") // open the file
{
$ Sess_data = fread ($ fp, filesize ($ sess_file); // read the file
Return ($ sess_data); // return the read content
}
Else
{
Return (""); // if the Read fails, a null value must be returned.
}
}
Function write ($ id, $ sess_data) // defines the write function
{
Global $ sess_save_path, $ sess_session_name; // predefined save path and name
$ Sess_file = "$ sess_save_path/sess _ $ id"; // defines the file
If ($ fp = @ fopen ($ sess_file, "w") // open the file
{
Return (fwrite ($ fp, $ sess_data); // execute the write operation
}
Else
{
Return (false); // An error is returned if opening fails.
}
}
Function destroy ($ id) // defines the deregister function
{
Global $ sess_save_path, $ sess_session_name;
$ Sess_file = "$ sess_save_path/sess _ $ id"; // specify the file
Return (@ unlink ($ sess_file); // delete the session file
}
Function gc ($ maxlifetime) // defines the expiration function
{
Return true; // returns the true value directly.
}
Session_set_save_handler ("open", "close", "read", "write", "destroy", "gc"); // sets the function
Session_start (); // initialize the session
// You can continue to use the session normally below

I hope this article will help you with PHP programming.

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.