This article describes the PHP custom session save path and delete, logout and write methods. Share to everyone for your reference. The specific methods are as follows:
Copy Code code as follows:
$sessionpath =session_save_path (); Gets the save path for 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)//define Open functions
{
Global $sess _save_path, $sess _session_name; predefined session path and name
$sess _save_path= $save _path; Define Save Path
$sess _session_name= $session _name; Define Session Name
return (true); Return truth value
}
function close ()//define shutdown functions
{
return (true); return true Value directly
}
function read ($id)//definition read function
{
Global $sess _save_path, $sess _session_name; Predefined save paths and names
$sess _file= "$sess _save_path/sess_$id"; Definition file
if ($fp = @fopen ($sess _file, "R"))//Open File
{
$sess _data=fread ($fp, FileSize ($sess _file)); Reading files
Return ($sess _data); Return read content
}
Else
{
Return (""); Null value must be returned if read failed
}
}
function Write ($id, $sess _data)//define Write function
{
Global $sess _save_path, $sess _session_name; Predefined save paths and names
$sess _file= "$sess _save_path/sess_$id"; Definition file
if ($fp = @fopen ($sess _file, "w"))//Open File
{
Return (Fwrite ($fp, $sess _data)); Performing write operations
}
Else
{
return (false); If open failure returns an error
}
}
function Destroy ($ID)//Defining Logout functions
{
Global $sess _save_path, $sess _session_name;
$sess _file= "$sess _save_path/sess_$id"; Specified file
Return (@unlink ($sess _file)); Delete session file
}
Function GC ($MAXLIFETIME)//define expiration functions
{
return true; return true Value directly
}
Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC"); Set function
Session_Start (); Initializing session
You can continue to use the session normally as follows
I hope this article will help you with your PHP program design.