PHP Self defined Saving Path of session and Method of Deleting Logging Out and Writing
- 2021-08-03 09:33:47
- OfStack
In this paper, the methods of PHP self-defining session saving path, deletion, logout and writing are described. Share it for your reference. The specific methods are as follows:
$sessionpath=session_save_path(); // Gets the current session Save path of
echo $sessionpath;
if(strpos($sessionpath,";")!==false) // If there is a semicolon in the path
{
$sessionpath=substr($sessionpath,strpos($sessionpath,";")+1); // Set a new path
}
function open($save_path,$session_name) // Define open function
{
global $sess_save_path,$sess_session_name; // Predefined session Path and name
$sess_save_path=$save_path; // Define the save path
$sess_session_name=$session_name; // Definition session Name
return(true); // Return true value
}
function close() // Define the closing function
{
return(true); // Returns the true value directly
}
function read($id) // Define the read function
{
global $sess_save_path,$sess_session_name; // Predefined save path and name
$sess_file="$sess_save_path/sess_$id"; // Definition file
if($fp=@fopen($sess_file,"r")) // Open a file
{
$sess_data=fread($fp,filesize($sess_file)); // Read a file
return($sess_data); // Return to read content
}
else
{
return(""); // If the read fails, you must return a null value
}
}
function write($id,$sess_data) // Define a write function
{
global $sess_save_path,$sess_session_name; // Predefined save path and name
$sess_file="$sess_save_path/sess_$id"; // Definition file
if($fp=@fopen($sess_file,"w")) // Open a file
{
return(fwrite($fp,$sess_data)); // Perform a write operation
}
else
{
return(false); // If the opening fails, an error will be returned
}
}
function destroy($id) // Define the logout function
{
global $sess_save_path,$sess_session_name;
$sess_file="$sess_save_path/sess_$id"; // Specified document
return(@unlink($sess_file)); // Delete session Documents
}
function gc($maxlifetime) // Define expired functions
{
return true; // Returns the true value directly
}
session_set_save_handler("open","close","read","write","destroy","gc"); // Set function
session_start(); // Initialization session
// The following can continue to be used normally session
I hope this article is helpful to everyone's PHP programming.