laravel cookie的实现:laravel工作会话和cookie
一:操作session1:session配置Session 的配置文件存储在config/session.php中,配置参数有:(1):配置session驱动'driver' => env('SESS
顺晟科技
2023-02-26 09:31:13
71
Session配置文件存储在config/session.php中,配置参数如下:
& 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数组中,并且不会持续存在。
& AMP;# 039;lifetime & amp# 039;=env(& amp;# 039;session _ lifetime & amp# 039;120),
& amp;# 039;files & amp# 039;=存储//不修改基本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表。这是存储会话数据的表
$ 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数组数据的其他数据
session([& amp;# 039;key & amp# 039;=& amp# 039;value & amp# 039;]);//会话设置
会话()-push(& amp;# 039;arr & amp# 039;& amp# 039;item & amp# 039;);//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;){
//
}:值
$=会话(& 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;);
//导入会话数据和删除会话
$ 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();
//删除指定的会话
会话()-forget(& amp;# 039;name & amp# 039;);
//清除所有会话
会话()-flush();
$ request-session()-regenerate();
session()-regenerate();
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,则渲染视图时无法直接使用
cookie:get(&# 039;key & amp;# 039;);
cookie:forget(& amp;# 039;key & amp;# 039;);