PHP Performance Research --Session (1)

zhaozj2021-02-16  82

The meaning of session is dialogue. About SESSION how to use cookies, there is not much to say. We focus on the PHP's session mechanism.

1. SESSION's operation

PHP will check if the dialog is in Session_Start, and if not, it is not established.

Session_Register will make a modification for Session. Make a modification every time you assign the session.

It will also be modified when the sympathion is canceled.

2. Session store

GM has 2 storage methods. Files and databases.

Regardless of the file / database. This frequent operation of Session will cause the system's I / O load aggravation.

Comparison of files and databases:

The database is more convenient, and it is also true of management. And you can support multiple Server sharing session. But the efficiency is very low.

The file storage speed is very fast, the test is tested, and the SESSION directory structure using the 3 layer is best. But weakness is the session sharing inconvenience.

3. Transformation of Session's "Square".

Reduce frequent operations of session. You can make a value you need to store it in the session.

For example, $ _SESSION ['ID'] = 12345; $ _SESSION ['Name'] = 'ABCD'

We can completely combine: $ _ session ['mysession'] = 'ID: 5: 12345 | Name: 4: ABCD |' Store. This greatly reduces the I / O load.

4. Deep transformation of session.

Preliminary envision, modify the session module, further reduce the frequency of operation of the session, controlability of the session file. Enhance the use of file sess, multiple Server sharing. Reduce the frequentity of database connections (long connection?) When using the file database store SSSION

2004-07-23 Supplement with the "skin shallow" transformation of session.

Class mysession {var $ g_dt_seesion = false; var $ mysess = array (); var $ seestr = '; var $ errno = 0; var $ register = false;

Function mysession () {if (! $ this-> g_dt_seesion) {if (! session_start ()) {$ this -> _ set_errno (103); return false;} $ this-> g_dt_seesion = true; if (iSset ($ _ session ['MyWebSession'])) {$ this-> register = true; $ this-> seestr = $ _SESSION ['mywebsession ";} else {session_register (' mywebsession ');}} f ($ this-> register) {$ This -> _ split_sess ();}} function _set_errno ($ no) {$ this-> errno = $ no;} function set_val ($ k, $ v) {if ($ this-> errno) Return False; $ This-> Mysess [$ K] = $ V;} Function Get_VAL ($ K) {Return $ this-> Mysess [$ K];}

Function _Split_SESS () {$ C = Strlen ($ this-> SESTR); $ FLAG = 0; for ($ I = 0, $ V = 1, $ x = 0; $ i <$ C; $ i ) {Switch ($ V) {Case 2: IF ($ this-> Sestr {$ I} == ':') {$ vallen = 0; $ Vallen = Substr ($ this-> SESTR, $ I - $ X, $ x); IF ($ Vallen> 0) {$ I ; $ Valstr = Substr ($ THIS-> SESTR, $ I, $ VALLEN); $ I = $ VALLEN - 1;} else {$ valstr = '; } $ = 0; $ = 0; $ V = 0; $ flag ;} else {$ x ;} Break; case 1: if ($ this-> seestr {$ i} == ':') {$ Keystr = Substr ($ this-> Sestr, $ I - $ x, $ x); $ x = 0; $ V ;} else {$ x ;} Break; Case 0: Default: IF ($ 1. == '|') {$ x = 0; $ V ;} Break;} if ($ this-> seestr {$ I} == "/ n")} == "/ n") {BREAK }}} Function is_ok () {return! $ This-> errno;} function my_dump () {=-> errno Return False; Foreach ($ this-> MySESS AS $ K => $ V) {$ Str. = $ k. ':'. Strlen ($ V). ':'. $ v. '|';} $ _SESSION ['mywebsession'] = $ STR; } function get_session_id () {return session_id ();} function my_session_unregister ($ k) {session_unregister ($ k);} function my_session_unset () {session_unset ();} function err_msg () {switch ($ this-> errno) { Case 103: return 'session start error'; default: return;}}}

$ cc = new mySession (); $ cc-> set_val ('aaa', '1111'); $ cc-> set_val ('bbb', '222'); $ cc-> my_dump (); Echo $ CC- > get_session_id (). ""; / * Echo $ cc-> get_val ('aaa'); Echo $ CC-> Get_VAL ('bbb'); * / echo session_save_path ();> Suitable for multi-session storage. Of course there is room for transformation.

转载请注明原文地址:https://www.9cbs.com/read-14035.html

New Post(0)