Core PHP Programming - Technical Session

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yangmolulu/article/details/91476826

Technical Session

table of Contents

Technical Session

Preliminary technical knowledge session

Classification of technical sessions

Session difference between the two technologies

The basic use of Cookie

Cookie Principle

The basic use of COOKIE

COOKIE Advanced Use

COOKIE life cycle

COOKIE scope

COOKIE across subdomains

COOKIE array data

Basic use SESSION

SESSION principle

Basic use SESSION

SESSION configuration

Two forms of configuration

Destruction SESSION

SESSION garbage collection

After disabling COOKIE how to use SESSION


Preliminary technical knowledge session

Introduction session technology : web sessions can be simply understood as follows: the user to open a browser to access a particular web site, click on the site in multiple hyperlinks to access multiple web server resources, then close the browser, the whole process is called a session.

 

Features HTTP protocol is a connectionless / stateless, when a browser repeatedly request the same web server, the server is unable to distinguish whether multiple operations from the same browser (user), technical session is to find ways to make the HTTP protocol from the same server to recognize a browser's repeated requests, thus facilitating the browser (user) at the same site multiple operation can be continued without the need for additional authentication.

 

Classification of technical sessions

1) cookie technology

Cookie is in the HTTP protocol, a server or workstation on a script can be a way to maintain customer information.

Cookie is a web server stored in a user's browser (client) small text file on (HTTP protocol response headers), which can contain information about the user. Whenever a user connects to the server (HTTP request carries the data), web site can be accessed Cookie information.

2) session technology

Session refers to a time interval end users communicate with an interactive system, usually from the registration into the system to the elapsed time between log out of the system and, if necessary, may have some room for maneuver. Session technique is to save data to the server whenever the user connects to the server, web site can access the Session Info: achieve SESSION COOKIE technology is dependent on technology.

 

Session difference between the two technologies

1) safety

       a) Session stored on the server side, safe

       b) Cookie is stored in the browser, security is low

2) the data size aspect

       a) Cookie limit both the number and size (20 / 4k)

       b) Session data storage Any

3) available data types

       a) Cookie can simply store data: value / string

       b) Session data may be stored complex (automatic serialization)

4) Save in respect

a) Cookie saved on the browser

       b) Session saved on the server

 

The basic use of Cookie

Cookie Principle

Technical COOKIE: HTTP server by storing the data to the browser, the browser may carry COOKIE data corresponding to the access server at a later response.

1, when the first request, PHP setcookie by head transmission function data in response to the browser via the http protocol

2, the first time the browser will save the Cookie response data to the browser

3, subsequent browser requests for the same site, Cookie will automatically detect the presence of data, if there is a request to a server that carries data in the header.

4, when the execution of PHP browser will automatically determine whether the request carries Cookie, if any, is automatically saved to the $ _COOKIE.

5, using the $ _COOKIE Cookie data access

The basic use of COOKIE

Set COOKIE information

setcookie (string $ name [, string $ value = ""]) function is used to set information COOKIE

Type requires cookie value: must be simple string or integer type

 

COOKIE read information

1)$_COOKIE数组的使用

 

COOKIE(会话技术)能够实现跨脚本共享数据

 

COOKIE高级使用

COOKIE生命周期

COOKIE生命周期:COOKIE在浏览器生存时间(浏览器在下次访问服务器的时候是否携带对应的COOKIE)

 

1)默认(不设定)时的生命周期:不设定周期默认是关闭浏览器(会话结束)

2)设定一个常规日期戳的周期:通过setcookie 第三个参数可以限定生命周期,是用时间戳来管理,从格林威治时间开始

 

 

3)设定为“0”的周期:在第三个周期设定生命周期的时候,用0代替时间戳:表示就是普通设置,会话结束过期。

4)删除一个cookie做法:服务器没有权限去操作浏览器上的内容(不可能删除)。可以通过设定生命周期来让浏览器自动判定cookie是否有效,无效就删除。    

4.1 清空cookie数据内容

 

 

4.2 设定时间戳过期

COOKIE作用范围

作用范围:在不同的文件夹层级中,设定的COOKIE默认是在不同的文件夹下有访问限制。上层文件夹中设定的COOKIE可以在下层(子文件夹)中访问,而子文件夹中设定的COOKIE不能在上层文件夹中访问。

 

1)默认(不设定)的范围:就是使用COOKIE默认的作用范围

  上层看不到下层内容,下层可以看到上层内容

2)设定为“/”的含义:告知浏览器当前COOKIE的作用范围是网站根目录

  setcookie(名字,值,生命周期,作用范围)

 

COOKIE跨子域

跨子域:在同一级别域名下,可以有多个子域名,它们之间是搭建在不同的服务器上(不同文件夹),但是可以通过COOKIE设置实现对应的COOKIE共享访问。但是默认是不允许跨域名访问的

 

1)设定cookie有效域名:不同的域名(包含主机)之间不能共享COOKIE,可以通难过setcookie函数的第五个参数来控制:setcookie(名字,值,生命周期,作用范围,有效域名)

2)不设定时的默认有效域名

3)跨子域的设定方法:在设定域名访问的时候设定上级域名即可:myphp.com,这个是所有以myphp.com结尾的网站都可以共享COOKIE

 

COOKIE数组数据

COOKIE本身只支持简单数据(数字或者字符串),能够保留的数据本身有限,也不成体系。如果需要使用COOKIE来表示一组数据的话,想办法凑成数组,(COOKIE不支持数组)

 

1)设置形式:setcookie(‘c1[k1]’,值)

2)读取形式:$_COOKIE[‘c1’][‘k1’]

 

 

SESSION基本使用

SESSION原理

Session与浏览器无关,但是与Cookie有关。

1、PHP碰到session_start开启session会话,会自动检测sessionID

       a)如果Cookie中存在,使用现成的

       b)如果Cookie中不存在创建一个sessionID,并通过响应头以Cookie形式保存到浏览器上

2、初始化超全局变量$_SESSION为一个空数组

3、PHP通过sessionID去指定位置(session文件存储位置)匹配对应文件

       a)不存在该文件:创建一个sessionID命名文件

       b)存在该文件:读取文件内容(反序列化),将数据存储到$_SESSION中

4、脚本执行结束:将$_SESSION中保存的所有数据序列化存储到sessionID对应的文件中。

 

 

SESSION基本使用

启用session,任何时候都需要开启session(脚本使用到$_SESSION就开启一次)

 

$_SESSION是通过session_start()函数的调用才会定义的,没有直接定义。

 

SESSION使用需要开启session_start()

设置SESSION信息

如果想存储数据到session中,那么只要不断给$_SESSION数组添加元素即可。

 

 

读取SESSION信息

$_SESSION就是一个数组,存储什么数据,什么方式的,就可以通过什么方式访问什么数据

 

会话技术的本质是为了实现跨脚本共享数据:在一个脚本中定义数据,在另一个脚本中保存数据

 

删除SESSION信息

删除session就是将session数据清理掉(让$_SESSION拿不到)

 

删除一个SESSION信息

unset($_SESSION[元素下标])删除指定SESSION数据

 

删除全部SESSION信息

删除全部数据就是让$_SESSION变成有一个空数组

SESSION相关配置

SESSION基础配置(php.ini)

1)session.name :session名字,保存到COOKIE中sessionID对应的名字

2)session.auto_start:是否自动开启session(无需手动session_start()),默认是关闭的

3)session.save_handler:session数据的保存方式,默认是文件形式

4)session.save_path:session文件默认存储位置

使用系统的文件夹存储不安全,需要指定存储路径

 

SESSION常用配置(php.ini)

1)session.cookie_lifetime:PHPsessionID在浏览器端对应COOKIE的生命周期,默认是会话结束

2) session.cookie_path:sessionID在浏览器存储之后允许服务器访问的路径(COOKIE有作用范围)

 

3)session.cookie_domain:COOKIE允许访问的子域(COOKIE允许跨子域)

 

配置的两种形式

1)php.ini中配置:全局配置

2)脚本中配置:PHP可以通过ini_set函数来在运行中设定某些配置项(只会对当前运行的脚本有效)把这种配置称之为项目级

@ini_set(‘session.save_path’,路径)

 

销毁SESSION

SESSION删除是指删除SESSION数据,,$_SESSION中看不到而已。销毁session是指删除session对应的session文件。

系统提供了一个函数:session_destroy(),会自动根据session_start()得到的sessionID去找到指定的session文件,并把其删除。

 

SESSION垃圾回收机制

垃圾回收机制原理

Session会话技术后,session文件并不会自动清除,如果每天有大量session文件产生当时又都是失效的,会增加服务器的压力和影响session效率。

垃圾回收,是指session机制提供了一种解决垃圾session文件的方式:给session文件指定周期,通过session文件最后更改时间和生命周期进行结合判定,如果过期则删除对应的session文件,如果没有过期则保留。这样可以及时清理无效的僵尸文件,从而提升空间利用率和session工作效率。

 

1、任何一次啊session开启,session都尝试去读取session文件

2、读取session文件后,有可能触发垃圾回收机制(在session系统中也是一个函数,:自己有一定几率调用)

3、垃圾回收机制会自动读取所有session文件的最后编辑时间,然后加上生命周期(配置文件)与当前时间进行比较(所有session文件)

       a)过期:删除

       b)有效:保留

垃圾回收参数设置(php.ini)

1)session.gc_maxlifetime = 1440:规定的session文件最大的生命周期是1440秒,24分钟。

2 ) session.gc_probability = 1:垃圾回收概率因子(分子)

3 ) session.gc_divisor = 1000:垃圾回收概率分母

默认的触发概率是1/1000;

 

禁用COOKIE后如何使用SESSION

禁用COOKIE不能使用SESSION的原因

SESSION技术需要利用到COOKIE技术来保存sessionID,从而使得PHP能够在跨脚本的时候得到相同的sessionID,从而访问同一个session文件。

解决思路:最终让session_start在开启之前拿到原来的sessionID(另外一个脚本的)

 

实现无COOKIE使用SESSION

方案1:利用PHP提供的SESSION函数:session_id和session_name来获的和设置sessionID或者name从而解决session_start产生新sessionID的情况(手动操作)

       1、在session保存数据的脚本中获取session的id和名字

       2、想办法将数据传递给另一个脚本:表单传值(URL或者form表单)

       3、在需要使用session的脚本中,先接收数据

4、阻止session_start产生新的ID,告诉他已经存在:session_id($id),这时session_start不会再产生新的ID

方案2:可以利用session机制中已经提供的解决方案自动操作(配置)

原因1:默认session配置只允许使用COOKIE保存sessionID:session.use_only_cookies

原因2:默认关闭了其他能够传送数据的方式,只保留了cookie。

1、修改php.ini,开启其他方式传输sessionID,关闭只允许使用COOKIE传输功能

 

2、一旦配置开启,PHP会自动将sessionID和session名字在其他位置绑定数据,同时还会在session_start的时候,考虑其他方式(表单)传递的数据,而不是只有cookie

 

Guess you like

Origin blog.csdn.net/yangmolulu/article/details/91476826