This article describes how to customize session processing for php4. We provide an example of how to write a fully functional session processing program based on mysql database or dbm file. 1. the new php4 has a set of session processing functions. By default
This article describes how to customize session processing for php4. We provide an example of how to write a fully functional session processing program based on mysql database or dbm file.
I. Preface
The new php4 has its own session processing function. By default, each session is stored in an independent file in a temporary directory of the system (for example,/tmp in a unix system ).
This is suitable or inappropriate, depending on your needs. For example, if your web servers supporting php are distributed on different machines, you cannot easily share sessions between them (of course, you can also keep sessions in NFS sharing ). Another ambush title is that thousands or millions of session files on your machine make your file system messy.
We are honored that the php4 developers are very far-sighted (thanks to them) and they have provided interfaces for expanding session processing for users like you and me.
This document describes how to expand session processing and provides two examples of how to expand session processing. In our first example, the session handler will keep the session data in the DBM file. In our second example, session data is retained into the MYSQL database.
Before you start, download ying20000602.zip and release it to the web document directory. (I have brought it to the end of this article)
Any session handler we write will provide six basic functions which will be called by the session handler of php4, so you don't have to worry about how to call them.
Fortunately, these custom session processing functions are completely transparent to you. So you can correct them without affecting your own PHP scripts.
These functions are:
Sess_open ($ sess_path, $ session_name );
This function is called by the session handler for initialization. The two parameters to be passed to it are $ sess_path, which corresponds to the session. save_path option in your php. ini file; $ session_name, which corresponds to the session. name option in php. ini. For details about how they work, see the following example.
Sess_close ();
This function is called when the page ends and the session handler needs to be closed. (Be careful not to be confused with sess_destory. it is used to end the session)
Sess_read ($ key );
This function is used when the session handler reads the specified session key value ($ key.
This function retrieves and returns session data identified as $ key. (Note: You don't have to worry about how to serialize or deserialize the data. if you don't know what this means, don't worry about it)
Note: serialization is a technique used to keep variables or objects in files when the program ends or needs them. it is different from the method of retaining data only.
Sess_write ($ key, $ val );
This function is called when the session handler needs to retain data, which is often generated at the end of your program.
It is responsible for keeping the data in the location where the sess_read ($ key) function can be searched next time.
Sess_destroy ($ key );
This function is required to destroy the session. It is responsible for deleting sessions and clearing the environment.
Sess_gc ($ maxlifetime );
This function is responsible for clearing fragments. In this case, it is responsible for deleting expired session data. Session handlers call them by chance.