Home > Article > Backend Development > Look at PHP's session mechanism from two handlers_PHP tutorial
1. session_start()
session_start() is the beginning of the session mechanism. It has a certain probability of turning on garbage collection. Because the session is stored in a file, PHP's own garbage collection is invalid. SESSION recycling requires deleting files. This probability is based on It is determined by the configuration of php.ini, but some systems have session.gc_probability = 0, which means the probability is 0, and garbage collection is implemented through cron scripts.
session.gc_probability = 1 session.gc_divisor = 1000 session.gc_maxlifetime = 1440//过期时间 默认24分钟 //概率是 session.gc_probability/session.gc_divisor 结果 1/1000, //不建议设置过小,因为session的垃圾回收,是需要检查每个文件是否过期的。 session.save_path = //好像不同的系统默认不一样,有一种设置是 "N;/path" //这是随机分级存储,这个样的话,垃圾回收将不起作用,需要自己写脚本
session will determine whether there is currently $_COOKIE[session_name()]; session_name() returns the COOKIE key value that saves session_id. This value can be found from php.ini session.name = PHPSESSID //Default value PHPSESSID
If it does not exist, a session_id will be generated, and then the generated session_id will be passed to the client as the value of COOKIE. It is equivalent to performing the following COOKIE operation. Note that this step performs the setcookie() operation, and COOKIE is in the header Sent in the header, there will be no output before this. PHP has another function session_regenerate_id(). If you use this function, there will be no output before this.
setcookie(session_name(), session_id(), session.cookie_lifetime,//默认0 session.cookie_path,//默认'/'当前程序跟目录下都有效 session.cookie_domain,//默认为空 )
If it exists, then session_id = $_COOKIE[session_name]; Then go to the folder specified by session.save_path to find the file named 'SESS_' . session_id(). Read the contents of the file, deserialize it, and put it in $_SESSION.
2. Assign value to $_SESSION
For example, add a new value $_SESSION['test'] = 'blah'; then this $_SESSION will only be maintained in memory. When the script execution ends, use the value of $_SESSION to be written to the specified session_id folder, and then close the related resources.
At this stage, it is possible to perform operations to change the session_id, such as destroying an old session_id and generating a new session_id. Half of it is used for custom session operations and role conversion, such as Drupal. Drupal anonymous users have a SESSION Yes, it needs to use a new session_id after logging in.
if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time() - 42000, '/');//旧session cookie过期 } session_regenerate_id();//这一步会生成新的session_id //session_id()返回的是新的值
3. Write SESSION operation
At the end of the script, the SESSION write operation will be performed, and the value in $_SESSION will be written to the file named by session_id. It may already exist, and a new file may need to be created.
4. Destroy SESSION
The COOKIE sent by SESSION is generally an instant COOKIE and is stored in memory. It will expire when the browser is closed. If you need to force the expiration manually, such as logging out instead of closing the browser, then you need to add it in the code. There are many ways to destroy SESSION:
When refreshing again without closing the browser, cookies 2 and 3 will be sent, but the data cannot be found.
User-defined session processing mechanism, more intuitive session_set_save_handler('open', 'close', 'read', 'write', 'destroy', 'gc');
session_start(), execute open($save_path, $session_name) to open the session operation handle. $save_path is session.save_path when session.save_handler = files, but if the user customizes it, neither of these two parameters will be used, and TRUE will be returned directly. Execute read($id) to read data from it. //This parameter is automatically passed to session_id(), and you can operate through this value.
The script execution ends, execute write($id, $sess_data) //Two parameters, very simple
If the user needs session_destroy(), execute destroy first and then step 2
A practical example:
//SESSION初始化的时候调用 function open($save_path, $session_name) { global $sess_save_path; $sess_save_path = $save_path; return(true); } //关闭的时候调用 function close() { return(true); } function read($id) { global $sess_save_path; $sess_file = "$sess_save_path/sess_$id"; return (string) @file_get_contents($sess_file); } //脚本执行结束之前,执行写入操作 function write($id, $sess_data) { echo "sdfsf"; global $sess_save_path; $sess_file = "$sess_save_path/sess_$id"; if ($fp = @fopen($sess_file, "w")) { $return = fwrite($fp, $sess_data); fclose($fp); return $return; } else { return(false); } } function destroy($id) { global $sess_save_path; $sess_file = "$sess_save_path/sess_$id"; return(@unlink($sess_file)); } function gc($maxlifetime) { global $sess_save_path; foreach (glob("$sess_save_path/sess_*") as $filename) { if (filemtime($filename) + $maxlifetime < time()) { @unlink($filename); } } return true; }