ThinkPHP关于session的操作方法汇总_php实例
本文详细讲述了ThinkPHP关于session的各种操作方法,详情如下:
ThinkPHP操作session官方的说明文档如下:
start 启动session
pause 暂停session
clear 清除session
destroy 销毁session
get 获取session值
getLocal 获取私有session值
set 设置session值
setLocal 设置私有session值
name 获取或者设置session_name
is_set 是否设置session值
is_setLocal 是否设置私有session值
id 获取或者设置session_id
path 获取或者设置session_save_path
setExpire 设置session过期时 间
setCookieDomain 设置有效域名
setCallback 设置Session 对象反序列化时候的回调函数
最常用的操作方法示例代码如下:
// 检测Session变量是否存在 Session::is_set('name'); // 给Session变 量赋值 Session::set('name','value'); // 获取Session变量 Session::get('name');
和Session相关的配置参数代码如下:
'SESSION_NAME'=>'ThinkID', // 默认Session_name 'SESSION_PATH'=>'', // 采用默认的Session save path 'SESSION_TYPE'=>'File', // 默认Session类型 支持 DB 和 File 'SESSION_EXPIRE'=>'300000', // 默认Session有效期 'SESSION_TABLE'=>'think_session', // 数据库Session方式表名 'SESSION_CALLBACK'=>'', // 反序列化对象的回调方法
其中SESSION_NAME 参数需要注意,如果需要在不同的项目之间不共享传递Session的值,请设置不同的值,否则请保留相同的默认值。
如果设置了相同的SESSION_NAME的值,但是又希望创建基于项目的私有Session空间,应该怎么处理呢?ThinkPHP还支持以项目为 Session空间的私有Session操作,以之前的常用操作为例,我们更改如下:
// 检测Session变量是否存在(当前项目有效) Session::is_setLocal('name'); // 给Session变 量赋值(当前项目有效) Session::setLocal('name','value'); // 获取Session变量(当前 项目有效) Session::getLocal('name');
这样,和全局的Session操作就不会冲突,可以用于一些特殊情况的需要。
ThinkPHP支持数据库方式的Session操作,设置SESSION_TYPE的值为DB就可以了,如果使用数据库方式,还要确保设置好SESSION_TABLE的值,并且导入下面的DDL到你的 数据库(以MySQL为例子):
CREATE TABLE `think_session` ( `id` int(11) unsigned NOT NULL auto_increment, `session_id` varchar(255) NOT NULL, `session_expires` int(11) NOT NULL, `session_data` blob, PRIMARY KEY(`id`) )
注意,Db Session方式的数据库连接会采用项目的数据库配置信息进行连接。除了数据库方式外,还可以增加其它方式的Session保存机制,例如内存方式、 Memcache方式等,我们只要增加相应的过滤器就行了,使用session_set_save_handler 方法,具体的方法定义参考Think.Util.Filter下面的FilterSessionDb.class.php 文件的实现。
制作了一个简单的登陆判断
登陆检测之后赋予Session值,使Session的值为非空即为假的false
$_SESSION[C('USER_AUTH_KEY')] = $logInFind['id'] ;
其中 [C('USER_AUTH_KEY')]为ThinkPHP的内置方法和函数类。在未配置config.php文件时默认为空
把$logInFind['id'] 取出的帐号值赋予它,默认为关闭页面Session就自动删除消失!
其它页面使用下面格式判断
if(!isset($_SESSION[C('USER_AUTH_KEY')])) { //isset 是检测变量是否赋值! $this->redirect('Login','Login'); //转到注册页面 }

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

AI Hentai Generator
Generate AI Hentai for free.

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.

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

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.

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.

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

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.
