TP5 Session和Cookie
application\index\controller\Index.php
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index()
{
/*
* session 函数助手
*
// 赋值(当前作用域)
session('name', 'thinkphp');
// 赋值think作用域
session('name', 'thinkphp', 'think');
// 判断(当前作用域)是否赋值
session('?name');
// 取值(当前作用域)
echo session('name');
// 取值think作用域
echo session('name', '', 'think');
// 删除(当前作用域)
session('name', null);
// 清除session(当前作用域)
session(null);
// 清除think作用域
session(null, 'think');
*/
session('session_name', 'onestopweb1');
session('sessionname.item', 'onestopweb2');
/*
* cookie 函数助手
*
// 初始化
cookie(['prefix' => 'think_', 'expire' => 3600]);
// 设置
cookie('name', 'value', 3600);
// 判断
cookie('?name');
// 获取
echo cookie('name');
// 删除
cookie('name', null);
// 清除
cookie(null, 'think_');
*/
cookie('cookieName', 'onestopwebCookie', 3600);
return $this->fetch();
}
}
application\index\view\index\index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Session和Cookie</title>
<!-- TP5内置标签:资源文件加载 -->
{css href="/asset/css/base.css" /}
{js href="/asset/js/jquery.js" /}
<style type="text/css">
p{ text-align: center;}
</style>
</head>
<body>
<p>session 取值</p>
<p>{$Request.session.session_name}</p>
<p>{$Request.session.sessionname.item}</p>
<p>cookie 取值</p>
<p>{$Request.cookie.cookieName}</p>
</body>
</html>
效果图:
