PHP Custom session save path and delete, logoff and write method _php tips

Source: Internet
Author: User
Tags define session

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.

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.