Php5 session Description 3: SESSION Security

Source: Internet
Author: User
The session module cannot guarantee that the information stored in the session can only be viewed by the user who created the session. Based on the data it stores, more measures need to be taken to actively protect the session integrity. Evaluating the data carried in a session and implementing additional protection measures usually costs you to reduce your convenience. For example, to protect users from simple social policies (

The session module cannot guarantee that the information stored in the session can only be viewed by the user who created the session. Based on the data it stores, more measures need to be taken to actively protect the session integrity. Evaluating the data carried in a session and implementing additional protection measures usually costs you to reduce your convenience. For example, to protect users from simple social policies (

The session module cannot guarantee that the information stored in the session can only be viewed by the user who created the session. Based on the data it stores, more measures need to be taken to actively protect the session integrity.

Evaluating the data carried in a session and implementing additional protection measures usually costs you to reduce your convenience. For example, if you want to protect users from simple social policies (note: The session ID displayed in the URL will be seen by others on the computer screen, or by another website through HTTP Referer), the session should be enabled. use_only_cookies. In this case, the client must enable cookie unconditionally, otherwise the session will not work. There are several ways to disclose the existing session ID to a third party. The leaked session ID allows a third party to access all resources associated with the specified ID.

First, the URL carries the session ID. If you connect to an external site, a URL containing a session ID may be stored in the Referer log of the external site.

Second, more active attackers may listen on packets in the network segment. If not encrypted, the session ID passes through the network in plain text. The solution is to implement SSL on the server and force the user to use it. By default, all data related to a specific session is stored in a file in the directory specified by session. save_path Of The INI option. A file is created for each session (whether or not there is data related to the session ). This is because every time a session is opened, a file is created, whether or not data is written to the file. Note that due to restrictions on working with the file system, this behavior has a side effect, which may cause the loss of unstored data sessions in user-defined session processors (such as databases. The functions described above will be used in the following sections, but there are also some session-related functions:

Session_encode

Function: sesssion Information encoding

Function prototype: string session_encode (void );

Return Value: String

Function Description: The returned string contains the names and values of each variable in the global variable, in the form of a | s: 12: "it is a test \"; c | s: 4: "lala"; a is the variable name s: 12 represents the value of variable a "it is a test the length is 12 variables are separated by semicolons.

Session_decode

Function: decodes sesssion information.

Function prototype: boolean session_decode (string data)

Return Value: Boolean

Function Description: This function decodes session information. If the session information is successful, the logical value true is returned.

Php5 no longer uses session_id, but changes it into a constant SID and stores it in the cookie. If the cookie is disabled on the client, php automatically transmits the SID through the url transmission, provided that session. use_trans_sid = 1 in php. ini is set. In this case, it does not matter even if the client disables the cookie.

Use strip_tags () to output SID to avoid XSS-related attacks.

Cross-page Session transfer:

Session cross-page transmission requires three considerations:

1. The client has disabled cookies.
2. the browser has a problem and cannot access the cookie temporarily.
3. The session. use_trans_sid in php. ini is 0 or the -- enable-trans-sid option is not enabled during compilation.
Why? The reasons are as follows:
The Session file is divided into two parts: the session variable is stored on the server side (the session is stored as a file by default), and the session id is saved as a cookie on the client side. (Note: The session is cookie-based by default ). When your browser sends a request to the server, it also sends a cookie containing the session id (by default ). The server obtains the user's file based on the session id provided by the client, that is, the session variable value stored on the server. In fact, the session id can use the Cookie of the client or the Query_String of the Http1.1 protocol (that is, the "?" of the accessed URL To the server, and then the server reads the Session Directory ....... That is to say, session id is used to obtain the id card of the session variable stored in the service. When the code session_start (); is run, a session file is generated on the server, and a session id corresponding to it is also generated, define session variables to be stored in the generated session file in a certain form. The session id can be used to retrieve the Defined variables. After a cross-page session, you must execute session_start (); a session file is generated, and the corresponding session id is generated. (Note: A session id can be hidden if the user does not disable the Cookie, that is, no new session id will be generated, however, if the Cookie is disabled, You need to manually pass the session id to solve this problem, as shown in example 2 )), this session id cannot be used to retrieve the variables in the first session file mentioned above, because this session id is not the "key" to open it ". If the code session_id ($ session id) is added before session_start (); no new session file is generated and the session file corresponding to this id is directly read. By default, the session in PHP uses the Cookie of the client to save the session id. Therefore, when the cookie of the client fails, the session will be affected. Note that:

The session does not have to rely on cookies, which is a bit more advanced than the cookie. When the Cookie on the client is disabled or a problem occurs, PHP automatically attaches the session id to the URL, so that the session variable can be used across pages through the session id. However, such attachment also has certain conditions,

1. "session. use_trans_sid = 1 in php. ini or the -- enable-trans-sid option is enabled during compilation ";

Second, the server running PHP must be a unix/linux system. windows does not have this function.

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.