Deep implementation Session "includes session storage, session mechanism and use of sessions and cookies, perfect conversational mechanisms (also available in distributed machines)", Unlimited classification implementations

Source: Internet
Author: User

1.session Note points:
@session_start ();//This configuration needs to be noted that the session opening will have an impact, so use the error suppressor to limit "and use php.ini to automatically open the session"

Output problem before session_start ():
"The session information itself is added to the HTTP header information, which means that the HTTP body cannot be head"
Configure the output cache in php.ini, out_buffer Configuration "Note: The output is guaranteed to be cached in the script after it is turned on."

Note
(1) The key of the session variable in the script can only be the string type "$_session[10" is wrong, because the session itself is required to serialize "
(2) if Session_Start () is not declared, the $_session variable is equivalent to an ordinary array operation "does not interact with the session file"
(3) can not unset to delete the $_session to delete all session variables, but by setting the session to NULL to achieve "unset just read the session data is destroyed, the session file has no impact on the data"
(4) Delete session file "Session_destory () to achieve the destruction of the session file" "Only delete files, not the $_session variable processing" "Once the Session_destory operation is performed, The session write operation is no longer performed, even if there is a session variable exists "
Note: Delete the file and delete the SessionID (in cookie) after completely destroying the data associated with the current session.
"The session mechanism itself is the only thing to consider when deleting a session completely."

2.session of storage processing
Session.save_path to modify the path of the session file

When the request becomes more, the session file becomes more, how to manage at this time?
Multi-level subdirectory management via Session.save_path "Hierarchical management based on SessionID"

SessionID Formation algorithm: hash "Session.hash_bits_per_character = 5 (this means that the first level subdirectory has a total of 2^5=32 0-9,a-v)"

The management of the session file is still problematic:
Large file size, inconvenient management
Difficult to share "distributed server management issues"

Solution: Put session data into the database server for Save Management "processing of Session storage"


3.session Warehousing
The mechanism of reading and writing files in the session mechanism is involved in the management through the database.
"Set up a common class for all methods of Session entry"

Session_set_save_handle ();//The use of functions, combined with the session mechanism, to invoke the specified method
The use of this method, according to a certain order of the canonical call.
About the session of the file creation (storage does not need file creation) and the need to read the session and other operations of the mechanism or the session itself "so you need to use the specified session function: Session_set_save_ Handle and internal methods should be implemented specifically "
"Equivalent to the implementation of a registration function, in conjunction with the PHP internal mechanism to implement the session of the storage operation (just storage of the session mechanism itself need to create a file, the operation of the database instead)"
Session storage itself is to write the original text in the data, write to the database "to solve the session text too much, difficult to manage"


The format data type that is designed to be stored in a database table, because SessionID itself is a unique or primary key form.


Note
"To the session of the storage operation needs to be packaged separately, but also do not need to feel too difficult, that is, do not use the session of the file processing mechanism, the file to write files and other mechanisms to session_set_save_ Handle rewrite (Save the session basic mechanism (the time of session processing is not changed), only read and write to the scene is not the same), the session of the file processing moved to the database read and write to improve performance "
Session_set_save_handle: The order of execution requires attention
Initialization and destruction of open and close methods

4.session of garbage collection
The GC function in the session realizes the garbage collection of the session "because the session garbage collection is more special than the script, the script ends, PHP garbage collection, but the session needs to write the data before releasing"

If the session is stored, the unused SessionID saved in the session also needs to be recycled, which is also part of the session's garbage collection
"How to determine the principle of failure"
If the SessionID fails in the cookie, it will also invalidate the ID in the session (save SessionID in the cookie is refreshed, then the ID saved on the server cannot find the corresponding ID, then the ID on the server expires) " The number of failed SessionID actually occurs very much "
PHP handling SessionID is the principle of garbage: How long does PHP think an ID is not used, to determine whether it is garbage "garbage collection based on time: Default 1440s, modified by PHP configuration"


SESSION.GC related configuration is about session garbage disposal "Every time you turn on session_start () there is a probability of garbage collection"
Session_Start () looks for garbage collection, but only has the chance to do GC garbage collection, which is also to improve performance. Because it is not, every time Session_Start will implement GC garbage disposal "
"The process of garbage collection is judged by the time in the data in MySQL storage: The processing mechanism here is more manageable"


"Session_set_save_handle: The order of execution is reasonable, delete after reading, can ensure that even if the expiration or normal read (possible)"

"Focus!!! 】
"The order in which the session is processed after using the library requires its own mechanism for session processing"
(1) session_start (). Start with open "Operation initialization"
(2) performing read, reading
(3) It is possible to perform GC garbage collection
(4) During the execution of the script, the session_destory will be executed against the session variable and possibly
(5) The script execution is complete, and if session_destory is not executed, a write operation is performed
(6) After the script execution completes, the close operation

The above is the order in which the session is executed in the script


Common problems in 5.session
(1) The difference and connection between session and Cookie
Storage location, security level. The relationship between the two is also SessionID
(2) Validity of Session data
Browser is off.
The cookie stored in the request is SessionID invalid "default SessionID cookie variable is a temporary variable"
"It is important to note that if you do not persist the SessionID in the cookie, then restarting a browser is equivalent to getting a SessionID"
(3) Question 2 poses a problem: how to persist the session. "If the same user opens the same browser again, it will be able to get the same SessionID"
Extend the validity period of a cookie variable "persistent sessionid cookie variable"
As for the retention of the session itself and the persistence of the session is only part
"Only the data that is retained in the session is meaningful in the cookie about SessionID persistence."
"However, the length of time that the session is stored on the server should be consistent with the SessionID time set in the cookie."


Session_set_cookie_params ();//The use of this function
Setcookie ();///two functions are normal


(4) The cookie is disabled and the session cannot be "URL pass SessionID is not secure"
Session.use_only_cookie: The configuration for session use


These are all the questions of the session.

6. Design list: Classification management "Focus"
Infinite Classification "classification at the first level: for the head or for different levels of depth of the level of the show"
Tree list Hierarchy nodes: Node relationships are associated by a data table
"N Nodes (data), N-1 (data relations: associating by a field)"
Achieve Unlimited classification

Focus
How to contact the associated data "database implementation of the hierarchy of associations created!!!" 】
"SQL statement Lookup is key: SQL takes the value on that node and evaluates the parent node in any node, and returns the result"

The configuration when inserting the data also requires the corresponding addition of the class ID and the correlation ID, which is also the key when creating the classification


Focus
Using recursion to achieve infinite classification
"Get all the data at once, recursively find the data" "Save Database Overhead"
It is important to note that "although the idea of conditional judgment in SQL statements is easier to organize, this one-time operation, and then grouping the arrays for wireless classification display enables the function"
"As long as the idea of this array operation can be recursive, the first level of recursion is based on whether the parent ID is equal to the correlation ID in the child data, to recursive"
Recursive point and recursive exit recursion need to pay attention to the focus, and this wireless classification of the recursion is actually more simple, as long as the conditions for recursion can be, do not need to have a recursive exit "
"Recursion requires the sharing of all recursive results in a container, so you need to use static variables to do the array container"


Example:
<?php


One:
mysql_connect (' 127.0.0.1:3306 ', ' root ', ' 123456 ');
$sql = "SELECT * from Itcast_shop.it_category where 1 order by Sort_order";
$result = mysql_query ($sql);
while ($row = Mysql_fetch_assoc ($result)) {
$list [] = $row;
}
Echo ' <pre> ';
Var_dump ($list);


Second, recursive lookup
/**
*
* @param $arr Array of all current possible classifications to find subcategories within the array
* @param $p _id int the parent ID of the current lookup
*
* @param the depth of the current recursive call $deep int
*
* @return Array list sorted by array!
*/
function Gettree ($arr, $p _id, $deep =0) {
Use a static local variable to save all the elements that are found sequentially
Static $tree = Array ();
Traverse all possible categories to find parent_id== $p _id
foreach ($arr as $row) {
Determine if sub-classification
if ($row [' parent_id '] = = $p _id) {
is sub-category
Record the current found
$row [' deep '] = $deep;
$tree [] = $row;
Use the classification of the current search to find its sub-categories
Recursive invocation
Gettree ($arr, $row [' cat_id '], $deep + 1);
}
}

return $tree;
}

$tree = Gettree ($list, 0);

Var_dump ($tree);

foreach ($tree as $row) {
echo $row [' deep '];
echo str_repeat (' &nbsp;&nbsp; ', $row [' deep ']);//Duplicate a string
echo $row [' cat_name '];
Echo ' <br> ';
}

The above implementation of recursive call method "focus recursive implementation and recursive containers need to consider static variables to achieve"

Deep implementation Session "includes session storage, session mechanism and use of sessions and cookies, perfect conversational mechanisms (also available in distributed machines)", Unlimited classification implementations

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.