search
Homephp教程php手册The use of Session in ThinkPHP5

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设置成功');
    }
}

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');        // 读取二维数组
        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
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),