Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account


What are the skills of session session operation in PHP

2024-06-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >


Shulou( Report--

This article mainly introduces what are the skills of session conversation operation in PHP. It is very detailed and has certain reference value. Friends who are interested must finish it!

截屏2023-12-25 20

The details are as follows:

Conversational technology


Store session data with the server side, while making the session data distinguishing between browsers

An independent session data area is established for each session data (to store all the data of the current session), each session data area has a unique flag, and the browser side stores the unique identification pair use.

The session-id value given to the browser in response is also stored in the cookie data area on the browser side.


Session.auto_start = 0

Session_start ()

Additions, deletions, modifications and queries are all done using $_ SESSION.

You can manipulate the session data by manipulating the $_ SESSION array just like a normal array.

Session principle

The session-id stored in the browser-side cookie is an ordinary cookie variable.

Each session generates a session datazone stored on the server side

It is stored as a file by default in the temporary directory of the server system

Session.save_path ='/ temp'

Session data Properties

Validity period: one session cycle

Valid path: whole station

Valid domain: current domain

Whether to securely connect the transmission only: no

Whether HTTPOnly: no

The characteristics of the above session data are caused by the characteristics of the session-id stored in the browser cookie. It can be seen that if you need to change the properties of session data, you need to change the properties of the cookie variable PHPSESSID that stores session-id:

Php.ini has a setting for this property:

Secure connection transfer only:

;;session.cookie_secure =

Life cycle:

; Lifetime in seconds of cookie or, if 0, until browser is restarted.; = 0

Valid path:

; The path for which the cookie is valid.; = /

Valid domain:

; The domain for which the cookie is valid.; =


; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.; =

If you need to modify the default properties, you can do the following:

1. Modify php.ini configuration file

two。 Use ini_set (key,val) in the script to modify the setting configuration, which is only valid for the script cycle after setting, and needs to be set before session_start ().

3. Use specific functions:

/ / session_set_cookie_params (period of validity, valid path, valid domain, whether only secure transmission connection, whether HTTPOnly); session_set_cookie_paramas (60); session_start ()

Session grammar problems

Session data can be of any type, because the data in the session datazone is stored after serialization.

The subscript of $_ SESSION ['] can only be a string

Output should not exist before session_start ()

Session data area

Persists current session session data outside the script cycle

Use $_ SESSION to manage session data during the script cycle

Session destruction

/ / Delete the session datazone corresponding to the current session and close the session mechanism (cannot be persisted after the end of the cycle) session_destroy (); / / $_ SESSION still exists

Clear session data

$_ SESSION = array ()

Delete all data related to the current session:

Session_destroy (); unset ($_ SESSION); setcookie ('PHPSESSID', ", time ()-1); / / what PHPSESSID calls can be obtained through php.ini configuration / / the current value can be obtained through session_name (); Name of the session (used as cookie name).; = PHPSESSID

Rewrite session storage mechanism (storage, memory)


Easy to manage large amounts of session data

Convenient for web server clusters to share session data


Define custom related storage handling functions

Set it to the storage function required by the session mechanism (tell the session mechanism to use our function to complete the storage processing)

Session mechanism:

Session_set_save_handler ()

A total of six storage processing functions are required

Begin, end, read, write, del, gc (garbage collection)

Need to be used before session_start ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To



© 2024 SLNews company. All rights reserved.