session.save_handler = files
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:
- setcookie(session_name(), session_id(), time() - 8000000, ..);//Execute before logging out
- usset($_SESSION);//This will delete all $_SESSION data. After refreshing, COOKIE is passed, but there is no data.
- session_destroy();//This function is more thorough, delete $_SESSION, delete the session file, and session_id
When refreshing again without closing the browser, cookies 2 and 3 will be sent, but the data cannot be found.
session.save_handler = user
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; }

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
