亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 The use of Session in ThinkPHP5

The use of Session in ThinkPHP5

Oct 22, 2016 am 12:00 AM

Since I am used to the previous version of ThinkPHP, when I want to use Session, I directly use $_SESSION to access it. I read the ThinkPHP5 manual today and found out that it is not safe to use it this way. ThinKPHP5 encapsulates the Session, which at least seems much safer when used.

Session settings

If you want to operate Session, you need to use the ThinkSession class in Think PHP5

The code example is as follows:

namespace app\index\controller;
use think\Controller;
use think\Session;
class Index extends Controller{    
public function index()
    {        
        return $this->fetch();
    }    
    public function save($name='')
    {
        Session::set('user_name',$name);        
        $this->success('Session設(shè)置成功');
    }
}

Session reading

The safest way to read a Session is to use the session method of the ThinkRequet class

The sample code is as follows:

namespace app\index\controller;
use think\Request;
class User
{    
    public function index(Request $request)
    {   echo $request->session('user_name');        // 讀取二維數(shù)組
        echo $request->session('user.name');
    }
}

Using this method is not only safe but also allows you to read Session variables of any dimension.

Of course, you can also use the Session class to read the Session, but this method only supports reading of two-dimensional Session variables at most

Sample code:

namespace app\index\controller;
use think\Session;
class User{    
public function index()
    {        
        echo Session::get('user_name');        
        echo Session::get('user.name');
    }
}

Although it is a bit troublesome, it is not convenient to directly use the global array $_SESSION to store the session variable, but for safety, it is worth a try.

This article was first published on Dingqiu.com. Please indicate the source when reprinting.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72