Php session handling mechanism)

Source: Internet
Author: User
Tags php session
Php session processing mechanism (to) 1 .????? The default session storage method of the PHP server is file storage. on Windows, the default Session server file of PHP is stored in C: \ WINDOWS \ Temp. session_save_path ('. /t/'); specifies the specific storage directory. 2 .????? Session processing mechanism in S php (transfer)

1.
????? The default session storage method of the PHP server is file storage. on Windows, the default Session server file of PHP is stored in C: \ WINDOWS \ Temp. session_save_path ('. /t/'); specifies the specific storage directory.

2.
????? The COOKIE technology is used in the implementation of the SESSION. The SESSION will save a COOKIE containing session_id (SESSION number) on the client, and save other session variables on the server, such as session_name. When a user requests a server, the session_id is also sent to the server. the session_id is used to extract the variables stored on the server to identify who the user is. At the same time, it is not difficult to understand why the SESSION sometimes fails.
???? When the client disables cookies (click "tools"-"Internet options" in IE, and click "security"-"custom level" in the pop-up dialog box, set "allow COOKIE for each conversation" to disabled). session_id cannot be passed, and the SESSION becomes invalid. However, php5 can automatically check the cookie status on linux/unix platforms. if the client is disabled, the system automatically attaches session_id to the url for transmission. Windows host does not have this function.

3.
Session_start (): start a session or return an existing session.
The browser cannot output any output before Session_start () is used; otherwise, the following error occurs.
You can start session. auto_start = 1 in php. ini, so you do not need to call session_start () every time before using the session ().

4.
If session. auto_start = 1, the change of session_save_path ('./t/') is invalid. Because the next statement must be placed in front.

5.
The session file stored on the server does not encounter session_destroy (); and will not be deleted.
Even if the client browser is closed.
However, each session file generated by the server can ensure the randomness and uniqueness of the session file name.

6. Http://www.toplee.com/blog/300.html

Adds Session storage and processing capabilities for PHP.

Many PHPer may use the Session function provided by PHP to facilitate session processing. The Default Session storage method on the PHP server is file storage, on Windows, PHP stores default Session server files in C: \ WINDOWS \ Temp, and * NIX stores files in/tmp by default. if the number of concurrent accesses is large or the number of sessions is too large, there will be a large number of session files similar to sess_xxxxxx in these two directories. a large number of sessions in the same directory will lead to performance degradation and may lead to file system errors eventually caused by attacks. In this case, PHP provides a better solution.
Many friends may not have noticed the Session settings in php. ini:
;???? Session. save_path = "N; MODE;/path"


This setting provides a multi-level hash for the session storage directory. "N" indicates the directory level to be set, and "MODE" indicates the permission attribute of the Directory. the default value is 600, in WINDOWS, you do not need to set it. * you do not need to set it on NIX. The "/path" next to it indicates the root directory path of the session file. for example, we set it to the following format.
Session. save_path = "2;/tmp/phpsession"

The above settings indicate that the/tmp/phpsession directory is used as the root directory for storing php session files, and two levels of directories are hashed in this directory, each level of directory is 0-9 and a-z with a total of 36 letters and numbers as the directory name. in this way, the directory for storing sessions can reach 36*36. I believe that as a single server, this is enough. if your system architecture is designed to share session data with multiple servers, you can increase the directory level to three or more.
Note that php does not automatically create sub-directories by yourself. you need to create sub-directories by yourself. you can find the code for automatically creating directories on the Internet for your reference. The following code automatically creates three levels of sub-directories. you can modify them as needed.
Set_time_limit (0 );
$ String = '0123456789abcdefghijklmnopqrstuvwxyz ';
$ Length = strlen ($ string );
Function makeDir ($ param)
{
??? If (! File_exists ($ param )){
??????? MakeDir (dirname ($ param ));
??????? Mkdir ($ param );
??? }
}
For ($ I = 0; $ I <$ length; $ I ++ ){
??? For ($ j = 0; $ j <$ length; $ j ++ ){
??????? For ($ k = 0; $ k <$ length; $ k ++ ){
??????????? MakeDir ($ string [$ I]. '/'. $ string [$ j]. '/'. $ string [$ k]);
??????? }
??? }
}
?>

You may have noticed that the previous article mentioned the sharing of php sessions with multiple servers. this is a problem that many applications may encounter. There are also a lot of related resources on the internet. you can google this article, michael only mentions the general idea here.
Generally, we use the following two methods:
1. the NFS or Samba sharing method is simple and feasible to share the disk where session files are stored on each server.
2. centralized storage in the database. this is a lot of implementation methods. you can use the session_set_save_handler () function provided by php to redefine the session function. this method is recommended.

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.