• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

没有了
Prev

ThinkPHP6 杂项

Data: 2018-12-22 04:54:58Form: JournalClick: 10

# ThinkPHP6 杂项


# 一、Session

  • 要使用Session类必须使用门面方式( think\facade\Session )调用
  • 新版本不支持操作原生$_SESSION数组和所有session_开头的函数,只能通过Session类(或者助手函数)来操作

# 1、配置文件 session.php

return [
    // session name
    'name'           => 'PHPSESSID',
    // SESSION_ID的提交变量,解决flash上传跨域
    'var_session_id' => '',
    // 驱动方式 支持file cache
    'type'           => 'file',
    // 存储连接标识 当type使用cache的时候有效
    'store'          => null,
    // 过期时间
    'expire'         => 1440,
    // 前缀
    'prefix'         => '',
];

# 2、开启Session

  • 中间件 app\middleware.php 文件
\think\middleware\SessionInit::class

# 3、设置

  • session 支持多级数组操作
Session::set('name','欧阳克');
// Session数组
Session::set('admin.name','欧阳克');
Session::set('admin.uid',1);

# 4、读取

// 获取全部Session
Session::all();
// 获取Session中name的值
Session::get('name');

# 5、删除

Session::delete('name');

# 6、取值并删除

Session::pull('name');

# 7、登陆示例

  • 新建login.php文件
namespace app\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
class Login{
    public function index(){
        if(Request::method() == 'POST'){
            $all = Request::param();
            $admin = Db::table('shop_admin')->where('account',$all['account'])->find();
            if(empty($admin)){
                echo json_encode(['code'=>1,'msg'=>'未找到管理员']);
                exit;
            }
            if(md5($all['pwd']) != $admin['password']){
                echo json_encode(['code'=>1,'msg'=>'密码错误']);
                exit;
            }
            Session::set('uid',$admin['uid']);
            Session::set('account',$admin['account']);
            echo json_encode(['code'=>0,'msg'=>'登陆成功']) ;
        }else{
            $title = '商城';
            View::assign([
                'title'  => $title
            ]);
            return View::fetch();
        }
    }
}
  • 新建Login/index.html文件
<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
    <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
    <script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="background: #1E9FFF">
    <div style="position: absolute; left:50%;top:50%;width: 500px;margin-left: -250px;margin-top: -200px;">
        <div style="background: #ffffff;padding: 20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444;">
            <form class="layui-form">
                <div class="layui-form-item" style="color:gray;">
                    <h2>{$title}--后台管理系统</h2>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="account" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">&nbsp;&nbsp;&nbsp;&nbsp;</label>
                    <div class="layui-input-block">
                        <input type="password" id="password" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" onclick="dologin()">登录</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript">
        layui.use(['layer'],function(){
            $ = layui.jquery;
            layer = layui.layer;
            // 用户名控件获取焦点
            $('#account').focus();
            // 回车登录
            $('input').keydown(function(e){
                if(e.keyCode == 13){
                    dologin();
                }
            });
        });
        function dologin(){
            var account = $.trim($('#account').val());
            var pwd = $.trim($('#password').val());
            if(account == ''){
                layer.alert('请输入用户名',{icon:2});
                return;
            }
            if(pwd == ''){
                layer.alert('请输入密码',{icon:2});
                return;
            }
            $.post('/index.php/login/index',{'account':account,'pwd':pwd},function(res){
                if(res.code>0){
                    layer.alert(res.msg,{icon:2});
                }else{
                    layer.msg(res.msg);
                    setTimeout(function(){window.location.href = '/index.php/index/index'},1000);
                }
            },'json');
        }
    </script>
</body>
</html>
  • index/index.html文件
use think\facade\Session;
public function index(){
    $title = '商城';
    $session = Session::all();
    if(empty($session['uid'])){
        echo '<script type="text/javascript">alert("请登录!");window.location.href = "/index.php/login/index"; </script>';
        exit;
    }
    $login = $session['account'];
    # 左侧菜单
    $menu = Db::table('shop_menu')->where('fid',0)->select();
    $left = $menu->toArray();
    foreach($left as &$left_v){
        $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
    }
    # 右侧列表
    $param = Request::param();
    if(isset($param['status']) && $param['status'] == 1){
        $where['status'] = 1;
    }else if(isset($param['status']) && $param['status'] == 2){
        $where['status'] = 2;
    }else{
        $where = true;
    }
    $p = isset($param['p']) ? $param['p'] : 1;

    $db = new Goods();
    $order = [
        'add_time DESC',
        'id DESC'
    ];
    $right = $db->get_all($where,$order,$p,5);
    View::assign([
        'title'  => $title,
        'login' => $login,
        'left' => $left,
        'right' => $right['data'],
        'count' => $right['count'],
        'p' => $p,
        'status' => isset($param['status']) ? $param['status'] : 0
    ]);
    return View::fetch();
}

# 二、Cookie

  • 要使用Cookie类必须使用门面方式( think\facade\Cookie )调用
  • 配置文件位于配置目录下的cookie.php文件,无需手动初始化,系统会在调用之前自动进行Cookie初始化工作
// 设置Cookie 有效期为 3600秒
Cookie::set('name', '欧阳克', 3600);

// 永久保存Cookie
Cookie::forever('name', '欧阳克');

//删除cookie
Cookie::delete('name');

// 读取某个cookie数据
Cookie::get('name');

# 2、Cookie 配置文件

  • config 目录下 cookie.php 文件
return [
    // cookie 保存时间
    'expire'    => 0,
    // cookie 保存路径
    'path'      => '/',
    // cookie 有效域名
    'domain'    => '',
    //  cookie 启用安全传输
    'secure'    => false,
    // httponly设置
    'httponly'  => false,
    // 是否使用 setcookie
    'setcookie' => true,
];

# 三、缓存

  • 要使用缓存必须使用门面方式( think\facade\Cache )调用
  • 内置支持的缓存类型包括file、memcache、wincache、sqlite、redis

# 1、使用缓存

// 缓存在3600秒之后过期

                
                
                
                
                
                
              
Name:
<提交>