tp5中的session的使用

開啓session


session('name', $user['username']);
session('id', $user['id']);

 

控制器器中首先是index控制器

<?php
namespace app\index\controller;
use think\Controller;
use think\Session;

use think\Session;//引入一下session文件

class Index extends Controller {

//頁面讀取頁面加載

public function index() {
      if(!Session::has('name')){
            //重定向到指定的URL地址
            $this->error('您未進行登錄', 'index/Login/index'); exit;
        }
         $user = Session::get('name');
        $this->assign('user', $user);
        return $this->fetch('index');
    }

 

//使用session更改密碼


    public function change() {
        $user = Session::get('name');
        //dump($user);exit;
        $this->assign('user', $user);
        return $this->fetch('change');
    }


    //更改密碼/命令

    public function alter() {
        if ($_POST) {
            $name = input('name');
            $former = input('former');
            $fresh = input('fresh');
            $affirm = input('affirm');
            $user = db('user')->where('name', $name)->find();
            if ($user['password'] == md5($former)) {
                $id = $user['id'];
                //                  dump($id);exit;
                $update = ['password' => md5($fresh) , 'id' => $id];
                $data = db('user')->update($update);
                if ($data) {
                    $out = session(null);
                    $this->success('更改成功', 'index/Login/index');
                } else {
                    $this->error('編輯失敗');
                }
            } else {
                $this->error('原始密碼不正確');
            }
        }
    }

//註銷退出登錄

 

   public function out() {
        $out =  Session::clear();
        $this->error('退出成功', 'index/Login/index');
    }

 

清空某個session

session('topic_all',null);//清空session

 

 

控制器獲取session值

$username = session('username');
dump($username);

 

視圖頁面讀取

<a>超級管理員 : {$Request.session.name}</a>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章