


Detailed introduction to application code examples of php session
Mar 22, 2017 am 09:42 AMThis article mainly introduces relevant information about the application details of php session. Friends who need it can refer to
php session advanced application
Session is very important in web technology. Since the web page is a stateless connection program, the user's browsing status cannot be known. Through the session, the user's relevant information can be recorded for confirmation when the user submits a request to the web server again in this capacity.
For example: When a user browses an e-commerce website, if there is no session, the user needs to enter the account password every time he browses.
1, Session temporary files
In the server, if all user sessions are saved in the temporary directory, the security and safety of the server will be reduced. efficiency. Opening a site stored on the server will be very slow.
Use PHP functionsession_save_path() function to store Session temporary files, which can alleviate the problems of reduced server efficiency and slow site opening caused by the storage of temporary files.
The sample code is as follows:
1 2 3 4 5 6 |
|
Note
session_save_path() must be executed before session_start().
2, Session Cache
Session caching is to temporarily store the content in the web page to the Temporary INternet Files folder of the IE client, and can Set the cache time.
Session cache uses the session_cache_limiter() function, whose syntax is as follows:
1 |
|
The parameter cache_limiter is public or private. Colleagues session is not on the server side, but on the client side. Not shown on the server.
To set the cache time, use the function session_cache_expire(). The syntax is as follows:
1 |
|
The parameter new_cahche_expire is the time number of the session cache, in minutes.
Note:
These two session functions must be executed before the session_start() function
Example of session cache page The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
The running results are as follows:
##3, Session database storage
bool session_set_save_handler(string open,string close,string read,string write,string destroy,string gc);
Object-orientedProgramming, everyone will have a clearer understanding.
1 2 3 4 5 6 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
library based on session_id, session_name, and expiration time. If the query result is not empty, modify the Session storage information in the database based on $key. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 |
|
(6) Encapsulate session_gc(), based on the Session expiration time Delete expired Session. The sample code is as follows:
1 2 3 4 5 6 |
|
The above is the detailed content of Detailed introduction to application code examples of php session. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
