18910140161

laravel cookie的实现:laravel工作会话和cookie

顺晟科技

2023-02-26 09:31:13

71

一:操作session

1:session配置

Session配置文件存储在config/session.php中,配置参数如下:

(1):配置session驱动

& AMP;# 039;驱动程序和amp# 039;=env(& amp;# 039;SESSION _ DRIVER & amp# 039;& amp# 039;file & amp# 039;),//#其中。首先读取env文件的session _;

File-将Session保存到指定的文件地址。

cookie-会话存储在安全加密的cookie中。

Database-session存储在关系数据库中。

Memcached/redis-sessions存储在其中一个基于快速缓存的存储系统中。

Array-sessions存储在PHP数组中,并且不会持续存在。

(2):配置失效时间

& AMP;# 039;lifetime & amp# 039;=env(& amp;# 039;session _ lifetime & amp# 039;120),

(3):使用file驱动时session文件保存地址

& amp;# 039;files & amp# 039;=存储//不修改基本redis

(4):使用redis驱动时指定 Session 使用哪个 Redis 连接

& amp;# 039;table & amp# 039;=& amp# 039;sessions & amp# 039;使用和使用数据库驱动程序时,从目录命令行运行

Php artisan session:table在数据库/迁移目录中创建用于创建会话数据表的文件

/* *

* Run the migrations。

*

* @return void

*/

Public function up()

{

模式:create(& amp;# 039;sessions & amp# 039;function (blueprint $ table) {

$ table-string(& amp;# 039;id & amp# 039;)-unique();

$ table-unsigned biginteger(& amp;# 039;user _ id & amp# 039;)-nullable();

$ table-string(& amp;# 039;IP _ address & amp# 039;45)-nullable();

$ table-text(& amp;# 039;user _ agent & amp# 039;)-nullable();

$ table-text(& amp;# 039;payload & amp# 039;);

$ table-integer(& amp;# 039;last _ activity & amp# 039;);

});

}

/* *

* Reverse the migrations。

*

* @return void

*/

Public function down()

{

模式:dropifexists(& amp;# 039;会话& amp# 039;);

}然后运行

现在,PHP artisan migrate在数据库中创建sessions表。这是存储会话数据的表

(5):使用database驱动时将session数据存入的数据库名

2:设置session

$ request-session()-put(& amp;# 039;key & amp# 039;& amp# 039;value & amp# 039;);//会话设置

$ request-session()-push(& amp;# 039;arr & amp# 039;& amp# 039;item & amp# 039;);//session数组数据的其他数据

(1):使用Request实例设置session

session([& amp;# 039;key & amp# 039;=& amp# 039;value & amp# 039;]);//会话设置

会话()-push(& amp;# 039;arr & amp# 039;& amp# 039;item & amp# 039;);//session数组数据的其他数据

(2):使用全局辅助函数session设置

3:获取session

$ data=$ request-session()-all();//导入所有会话

$ value=$ request-session()-get(& amp;# 039;key & amp# 039;);//获取指定的会话

////获取指定的会话,无默认设置

$ value=$ request-session()-get(& amp;# 039;key & amp# 039;& amp# 039;default & amp# 039;);

$ value=$ request-session()-get(& amp;# 039;key & amp# 039;function () {

Return & amp# 039;default & amp# 039;

});

确定//Session是否具有值。如果存在值且不为null,则has方法返回true

if($ request-session()-has(& amp;# 039;key & amp# 039;){

//

}

确定//Session是否具有值。即使值为null,也返回true

if($ request-session()-exists(& amp;# 039;key & amp# 039;){

//

}:值

(1):使用Request实例获取session

$=会话(& amp# 039;key & amp;# 039;);

$ value=session(& amp;# 039;key & amp# 039;& amp# 039;default & amp# 039;);

$ value=session()-get(& amp;# 039;key & amp# 039;)

$ value=session()-get(& amp;# 039;key & amp# 039;& amp# 039;default & amp# 039;)

会话()-has(& amp;# 039;key & amp;# 039;);

会话()-exists(& amp;# 039;key & amp;# 039;);

(2):使用全局辅助函数session获取

4:删除session

//导入会话数据和删除会话

$ value=$ request-session()-pull(& amp;# 039;key & amp# 039;& amp# 039;default & amp# 039;);

//删除指定的会话

$ request-session()-forget(& amp;# 039;key & amp# 039;);

//删除所有会话

$ request-session()-flush();

(1):使用Request实例删除session

//删除指定的会话

会话()-forget(& amp;# 039;name & amp# 039;);

//清除所有会话

会话()-flush();

(2):使用全局辅助函数session删除

5:重新生成 Session ID

$ request-session()-regenerate();

(1):使用Request实例重新生成 Session ID

session()-regenerate();

(2):使用全局辅助函数session重新生成 Session ID

二:操作cookie

cookie:queue(& amp;# 039;测试& amp# 039;& amp# 039;测试值& amp# 039;10);//设置cookie,test值为testValue,设置cookie时间为10分钟

cookie:make(& amp;# 039;cookie名称& amp# 039;& amp# 039;值& amp# 039;10);//cookie设置,cookie _ name值为value,cookie设置为10分钟

cookie:forever(& amp;# 039;key & amp# 039;& amp# 039;value & amp# 039;);//设置cookie,键值value,永不过期注意:如果使用cookie:make()或cookie:forever()方法设置cookie,则渲染视图时无法直接使用

1:设置cookie

cookie:get(&# 039;key & amp;# 039;);

2:获取cookie

cookie:forget(& amp;# 039;key & amp;# 039;);

  • TAG:
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航