[Reserved] depth understanding Session

What is the session

 The official definition of the session is: Session: on your computer, especially in network applications, known as "session control." Session object store particular user session configuration and the desired properties.

 To put it plainly session is a way to maintain data storage technology on the server side. session following main features:

 

 1. session is stored in the location server

 2. session cookie is generally to tie, in which case the browser's cookie functionality, will only be able to use URL rewriting to implement session storage functions

 3. Use a simple user session to maintain state, then when a large number of users simultaneously log on when or there are more number of session will lead to the problem of slow queries

 In essence: session temporarily store data technology is the technology is different from the back-end database based on

 

Why should session

 The main reason for this is HTTP statelessness

 Because when HTTP is stateless, so we can not send an HTTP request to know the current state of the user, that is, for example, such information which the user is currently the like, so this time we need to identify the current session status

 

seesion works

  Next, the initial session understood by a flowchart of the principle of simulated user login, it is assumed that when the user performs login operation, a particular session works as follows:

 

Probably this whole process is divided into steps:

1. The first step in a local cookie session identifier and a user name and password to the background

2. The second step to detect the background there is no corresponding session identifier, we php, for example, then that detects whether or not the received corresponding PHPSESSID

3. If not directly generate a new session. Yes, detecting the corresponding file exists and is effective

3. failure, we need to clear the session and then generate a new session. Do not fail, use the current session

See here you may have a preliminary understanding of the working principle of the session

session following schematic:

 

A common configuration of session

 We are here to configure PHP example to explain about the session

 首先我们要在PHP的安装目录下面找到php.ini文件,这个文件主要的作用是对PHP进行一些配置,具体以后涉及到再详讲。

  1. 设置session存放在cookie中中标识的字段名,php中默认为PHPSESSID

  对应的设置为:session.name = PHPSESSID

  2. 如果客户端禁用了cookie,可以通过设置session.use_trans_sid来使标识的交互方式从cookie变为url传递

    对应的设置为: session.use_trans_sid = 0

  3. 设置session的保存位置

  对应的设置是session.save_path="D:\phpStudy\PHPTutorial\tmp\tmp"

 

PHP中session实战

 首先我们需要安装wamp或者是phpstudy,具体方式自行百度

 为了方便观察session文件的变化,我们需要找到session的保存路径(在php.ini中找到session.save_path),如下:

 

 然后找到所指向的目录,注意一般来说session是使用files的形式来保存的,但是我们也可以根据自己的实际情况进行修改。我们可以在php.ini文件中进行修改和查看。

 

 使用session的第一步,我们要打开session,使用session_start(),然后我们给创建的session添加一个变量,我们假设为demo1,值为default ,代码如下:

复制代码
<?php
/**
 * Created by PhpStorm.
 * Date: 2017/12/16
 */
session_start();// 打开session
$_SESSION["demo1"] = "default";
?>
复制代码

 

执行效果如下:

 打开对应的文件,里面的内容如下:

 s:7 表示的是类型为string类型,长度为7个长度的字符串

 如果我们对session中的内容进行重新编辑的话,效果如下:

 我们观察最近一条的修改日期,我们可以发现就是日期发生了变化,但是文件名没有变化,也就是说,修改session中的内容不会导致文件被新建,而是执行对文件的重新写入操作

 session的销毁

 销毁session一般有两种方式,unset和session_destroy,我们先来说说第一种

 代码如下:

复制代码
<?php
/**
 * Created by PhpStorm.
 * Date: 2017/12/16
 */
session_start();// 打开session
$_SESSION["demo1"] = "default_1";
//session的销毁
unset($_SESSION);
?>
复制代码

 

这一个相当于没有删除session文件,但是使得即使有对应的PHPSESSID也无法获取到相应的session

session_destroy()相对来说比较彻底,直接删除对应的session文件

 

复制代码
<?php
/**
 * Created by PhpStorm.
 * Date: 2017/12/16
 */
session_start();// 打开session
$_SESSION["demo1"] = "default_1";
var_dump(session_name());
//session的销毁
session_destroy();
?>
复制代码

 

 

 

运行的效果如下:

对于个人来说比较推荐使用第二种方法,因为当要销毁session的时候,那么也就意味着session已经失效了,所以这个时候我们把它给删掉才是最好的处理方式,一方面可以减少对硬盘的存储,另外一方面可以相对优化session的查询速度。

 好了,这个时候我们应该要设置传递给浏览器端的cookie了,默认是自动传送,但是我们应该要学习的是怎样通过后端设置cookie过去

其中有两个方法与session有关的方法我们需要记住,第一个是session_name(),这个是获取cookie的key值得,第二个是session_id,这个是session的文件名

设置的示例代码:

复制代码
<?php
/**
 * Created by PhpStorm.
 * Date: 2017/12/16
 */
session_start();// 打开session
$_SESSION["demo1"] = "default_1";
setCookie(session_name(),session_id(),time()-1000);
?>
复制代码

 

在设置cookie的时候,我们为了程序的安全性,我们应该要禁止JS可以对cookie进行重写,所以需要设置HTTP ONLY,具体的设置方法在Php.ini中找到session.cookie_httponly

然后将其的值设置为1或者true即可

除此之外还可以通过setCookie和ini_set()来动态设置HTTPONLY属性

在使用session的时候,虽然会从浏览器把PHPSESSID传给后端,但是这个课程不需要人为的去参与。我们只需要保证HTTPONLY被设置就行了。下面是完整的代码:

 

复制代码
<?php
/**
 * Created by PhpStorm.
 * Date: 2017/12/16
 */
session_start();// 打开session
if ($_SESSION) {
    var_dump($_SESSION["demo1"]);
} else {
    $_SESSION["demo1"] = "default_" . time();
    var_dump($_SESSION["demo1"]);
    setCookie(session_name(), session_id(), time(), NULL, NULL, NULL, true);
}

?>
复制代码

 

 

 

 

session的一些相关注意事项

 1. 关闭浏览器session同样存在

 如果我们没有人为的去设置cookie的生命周期的时候默认关闭浏览器session的状态是无法被保存下来的,因为没有设置cookie的生命周期,默认这个时候cookie为session cookie也就是在会话存在的时候cookie才有效,所以关闭浏览器cookie失效,导致后端拿不到对应的PHPSESSID,所以无法找到对应的session文件

2.  session性能瓶颈怎样解决?

如果是后端存在大量的session的时候,那么这个时候就会出现性能的瓶颈,例如:当后端同时存在有5000个session文件的时候,假设要找的文件是在第4999个,那么也就是说前面至少需要遍历4998次,这样就会浪费过多的时间在后端的循环遍历查找文件中,所以这个时候最有效的方法是使用redis或者mongodb,原理是通过将原本保存在本地的session文件写入到内存中,通过内存换空间的形式来达到提升速度

3. 一般不使用URL重写的方法来传递PHPSESSID

其中主要有两个原因,一个是URL重写方式传递的话会导致URL混乱,影响美观。另一个是增大了用户误操作的几率

更多的session的相关配置请点击这里

  

更多的一些PHP.ini中session的含义

[Session]
session.save_handler = files  #session的存储方式
session.use_cookies= 1  #使用cookies在客户端保存会话
session.use_only_cookies = 1  #去保护URL中传送session id的用户
session.name = PHPSESSID  #session名称(默认PHPSESSID)
session.auto_start = 0  #不启用请求自动初始化session
session.cookie_lifetime = 0  #cookie存活时间(0为直至浏览器重启,单位秒)
session.cookie_path = /  #cookie的有效路径
session.cookie_domain =  #cookie的有效域名
session.cookie_httponly =  #httponly标记增加到cookie上(脚本语言无法抓取)
session.serialize_handler = php  #PHP标准序列化
 
session.gc_probability =1
session.gc_divisor =1000  #建议设置1000-5000
#概率=session.gc_probability/session.gc_divisor(1/1000)
#页面访问越频繁概率越小
session.gc_maxlifetime =1440  #过期时间(默认24分钟,单位秒)
 
session.bug_compat_42 = off  #全局初始化session变量
session.bug_compat_warn = off
session.referer_check =  #防止带有ID的外部URL
session.entopy_length = 0  #读取的字节
session.cache_limiter = {nocache,private,pblic}  #HTTP缓冲类型
session.cache_expire = 180  #文档过期时间(分钟)
session.use_trans_sid = 1  #trans_sid支持(默认0)
session.hash_function = 0  #hash方法{0:md5(128 bits),1:SHA-1(160 bits)}
session.hash_bits_per_character = 5  #当转换二进制hash数据奥可读形式是,每个字符保留位数
session.save_path =  "/var/lib/php/session"  #session id存放路径
转载自:https://www.cnblogs.com/st-leslie/p/8016951.html

Guess you like

Origin www.cnblogs.com/BOHB-yunying/p/11620121.html