In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the Session mechanism". In the daily operation, I believe that many people have doubts about what is the Session mechanism. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what is the Session mechanism?" Next, please follow the editor to study!
Session storage mode
In the php.ini file, configure.
Session.save_handler = files
Session.save_path = "Nathandom mode"
These two parameters can be set through ini_set in PHP without directly overwriting the values in the original php.ini.
Note:
N represents a multi-level directory and the value is a number.
MODE represents the permissions of the Session file created.
/ tmp represents the Session storage path.
The explanation on PHP's official website:
This directive also has an optional N parameter to determine the directory depth of the session file distribution.
For example, setting to '5alter TMP' will make the session file and path created similar to / tmp/4/b/1/e/3/sess_4b1e384ad74619bd212e236e52a5a174If.
To use the N parameter, you must create these directories before using them.
There is a small shell script called mod_files.sh,windows in the ext/session directory that mod_files.bat can use to do this.
Also note that if the N parameter is used and is greater than 0, then automatic garbage collection will not be performed, see php.ini for more information.
Also, if you use the N parameter, be sure to enclose the value of session.save_path in double quotes "quotes", because the delimiter semicolon (;) is also a comment symbol in php.ini.
The file storage module creates files using mode 600by default.
Change this default behavior by modifying the optional parameter MODE: Nathanath mode _ path, where MODE is the octal representation of mode.
The MODE setting does not affect the mask (umask) of the process.
Caution: note that for most sites, when using the optional directory level parameter N described above
A value greater than 1 or 2 is not appropriate-- because it requires a large number of directories to be created: for example, setting a value of 3 requires 64 ^ 3 directories on the file system, which wastes a lot of space and inode.
Set N greater than 2 only when you are absolutely sure that the site is large enough.
Session lifecycle
1. The first is through the program.
The session_destory () method clears all session
Unset (session ['x']) to clear the specified session ['x'].
two。 The second is by editing the browser
When closed, all session will be cleared directly.
Session garbage collection mechanism
Session.gc_maxlifetime
Session.gc_probability
Session.gc_divisor
Garbage Collection garbage collection:
Together, session.gc_divisor and session.gc_probability define the probability of starting the GC process when each session is initialized.
This probability is calculated by gc_probability/gc_divisor. For example, 1Plus 1000 means that there is a 0.1% chance of starting the GC process in each request.
GC's job is to scan all the session information, subtract the last modification time (modified date) of the session from the current time, and compare it with the session.gc_maxlifetime parameter
If the lifetime has exceeded the gc_maxlifetime, delete the session.
Session sharing
1. Redis [distributed]
2. Memcache [distributed]
Take Memcache as an example:
The advantage of sharing a memcache among multiple machines is that in the case of concurrent large access, it can also prevent a single machine, because the IO pressure caused by repeatedly creating and opening SESSION files, which has been tested before, is very effective for easing IO pressure.
/ / configure php.ini. Remember to restart after configuration
/ / memcache Ip (127.0.0.1) Port (11211)
Session.save_handler = memcache
Session.save_path = "tcp://127.0.0.1:11211"
/ / set_session.php
Session_start ()
$_ SESSION ['name'] =' Bihu'
Print $_ SESSION ['name']
Print "
"
Print session_id ()
/ / output after running
/ / Bihu
/ / 9325ag3rvunvhl7c2pfd3u27n0
/ / indicates that Session is stored successfully
/ / get_session.php
$memcache = new Memcache ()
$memcache- > connect ('127.0.0.1 dollars, 11211)
$data = $memcache- > get ('9325ag3rvunvhl7c2pfd3u27n0')
Print $data
/ / output after running
/ / name | SSession 4: "Bihu"; (you can parse the data [first understand the Session data structure])
/ / indicates that the Session was obtained successfully
/ / attach the method of parsing data
Function unserialize_php ($session_data)
{
$return_data = array ()
$offset = 0
While ($offset
< strlen($session_data)) { if (!strstr(substr($session_data, $offset), "|")) { throw new Exception("invalid data, remaining: " .substr($session_data, $offset)); } $pos = strpos($session_data, "|", $offset); $num = $pos - $offset; $varname = substr($session_data, $offset, $num); $offset += $num + 1; $data = unserialize(substr($session_data, $offset)); $return_data[$varname] = $data; $offset += strlen(serialize($data)); } return $return_data; } $un_data = unserialize_php($data); echo ''; var_dump(unserialize_php($un_data)); //输出数据 //array(1) { // ["name"]=>/ / string (4) "Bihu"
/ /}
At this point, the study on "what is the Session mechanism" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.