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.