ThinkPHP中Session用法详解_php实例
本文实例讲述了ThinkPHP中Session用法。分享给大家供大家参考。具体如下:
在ThinkPHP封装了Session类,用户可以直接使用,常用的方法有:
Session::set(name, value):注册 session 。
Session::is_set(name):检查Session的值是否设置。
Session::get(name):读取 session 。
Session::clear():清空Session。
Session::destroy():销毁 session 。
ThinkPHP 默认开启了 session 会话,因此在使用 Session 类之前不需要使用 session_start() 函数来开启会话.
使用 session 实例
下面以表单提交的方式注册 session,并在其他两个页面分别以模板方式和操作中以 Session::get 方式读出 session 值.
注册 session
User 模块 sessionTest 操作注册 session 例子:
public function session(){
if(!emptyempty($_POST['username'])){
Session::set('username',$_POST['username']);
}
$this->display();
}
}
sessionTest.html 模板(片段):
当填入用户名(如 testuser),点击 提交 按钮后,提交至 sessionTest 方法(即当前页面)处理并注册 session 值,以模板标签 present 来控制输出逻辑,检测到已经注册 $_SESSION['username'] 变量即输出:
testuser 你好! 首页 注销
否则会输出表单。
检测 session 是否注册
模板里检测
在模板里面,可以直接使用 present 或 notempty 甚至 switch 等标签判断 session 变量是否注册,以决定输出对应的 session 值(直接以输出数组单元的方式输出 session 变量值)或显示其他页面元素,具体可参见本教程模板标签部分内容以及上面的例子.
操作里检测
在操作里,可以使用 Session::is_set 方法检查 Session 的值是否设置,例如 User 模块 user 操作如下:
// 由于直接在操作里输出,为避免乱码
header("Content-Type:text/html; charset=utf-8");
if(Session::is_set('username')){
echo Session::get('username').' 你好';
}else{
echo 'session 未注册';
}
}
其他模块页面
在其他页面(如 Index/index)里,判断并读取 session 的代码片段:
session 无效(无法传递)
ThinkPHP 中可能出现 session 无效(无法传递至其他页面)的情况,可能的原因如下:
使用 Session 类头字母未大写,如:session::set.
页面有信息输出,如入口文件有空行等.
服务器上(Linux/Unix)的 session 存储路径(session.save_path)权限不正确导致无法将 session 信息正确存储.
作用域问题建议:
ThinkPHP 的 Session 类只是对 session 做了简单的包装,实际可以在操作中直接使用 PHP 原生的 session 函数,官方亦是如此建议.
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.
