This article mainly introduces how to rewrite the session of PHP storage mechanism, has a certain reference value, now share to everyone, the need for friends can refer to
Session Data Area
The default is stored as a file in the Server OS temp directory!
When the session data area is too large, the file form of storage, operation speed is slow. The disk read-write (io,input/output) overhead is significant.
In a real-world project, the session data is stored more quickly in other ways. Typical approach: Database, memory.
Take database storage As an example, explain: Session data Warehousing!
Rewrite the related operations directly to the session data area:
The most basic of only 2: Read, Write!
One: Define 2 functions that can be read and written.
Second: Inform the session mechanism, when the need to read and write, the use of user-defined read and write function to complete.
Definition 2 (actually a total of 6 required related functions) a function that can be read and written
Inform session mechanism, in the when you need to read and write, Use user-defined read-write function Completion
Session_set_save_handler (
Start function, End Function, read function, write function, delete function, GC function
);
Used to set user-defined functions as session store-related functions.
The above syntax, is only set to inform, not call the above 6 functions, these six functions, the session mechanism to run to a point in time, will be called! For example, when I open a session, I need to call Sessread ()
General Use session
Open session mechanism
Operation $_session
Create Session Table
In this session, each record is a session data area, which is equivalent to the original session file.
Table structure:
Read operation: Sessread ()
Who calls, who passes the argument!
When the function is called by the session mechanism of PHP, the current Session-id is passed as a parameter to the function:
Therefore, you need to set a formal parameter to accept the passed Session-id parameter:
Need to return, read to the session data string. is the contents of the Sess_content field. If it is not read, an empty string is returned, indicating that there is no session data.
Write operation: Sesswrite ()
When the phpsession mechanism calls the function to perform a write operation, the current Session-id and what needs to be written (serialized) are passed to the function!
2 parameters are required to receive:
Test:
Delete operation: Sessdelete ()
When the session is destroyed.
PHP functions were executed:
Session_destroy ();
Can destroy the session, delete the corresponding session data area, while closing the session mechanism!
Because you need to delete the session data area, you need to increase the method for deletion:
The session mechanism of PHP, when calling Sessdelete, will pass the current Session-id as a parameter:
You need to define formal parameters to receive:
Garbage collection Operation: SESSGC ()
Garbage: The obsolete session data area on the server. 、
How to determine the rubbish?
If a session data area has been used for longer than (the last write), it is considered junk data.
The time critical point: Default 1440s. Can be configured:
With the last write time, you can determine whether it is garbage
You need to add a field to record the last write time.
When writing, update the field:
Sesswrite ();
Judging condition: Expired
Last_write < current time-1440
How do I delete it?
During the session_start () process, the session mechanism is opened: the chance to perform a garbage collection operation. Once executed, all expired garbage data areas are deleted.
The default probability is 1/1000.
You can set this chance:
Possibility:
Cardinality (divisor):
Adjust odds test:
It is recommended to use the function Ini_set () during the script cycle to complete before opening the session mechanism:
Implement SESSGC ()
PHP session mechanism will be the maximum validity period as a parameter, pass over!
Start Operation Sessbegin ():
Initialization work
Guaranteed to be executed at the first one. The initial code, completed in Sessbegin:
For example, initializing a database connection:
End Operation Sessend ():
Finishing work
Return true;
Syntax details
First set in the session opening mechanism
Session_set_save_handler () is called before Session_Start ().
Do not automatically turn on session! Php.ini:session.auto_start = 0
PHP Configuration items: Session.save_handler
The storage mechanism used by PHP:
Finally, we recommend that you change the above configuration to User: users are customized!
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!