Home Backend Development PHP Tutorial ThinkPHP关于session的操作方法汇总_php实例

ThinkPHP关于session的操作方法汇总_php实例

May 16, 2016 pm 08:39 PM
session thinkphp operate

本文详细讲述了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');

Copy after login

和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'=>'',            // 反序列化对象的回调方法

Copy after login

其中SESSION_NAME 参数需要注意,如果需要在不同的项目之间不共享传递Session的值,请设置不同的值,否则请保留相同的默认值。
如果设置了相同的SESSION_NAME的值,但是又希望创建基于项目的私有Session空间,应该怎么处理呢?ThinkPHP还支持以项目为 Session空间的私有Session操作,以之前的常用操作为例,我们更改如下:

// 检测Session变量是否存在(当前项目有效)
Session::is_setLocal('name');
// 给Session变 量赋值(当前项目有效)
Session::setLocal('name','value');
// 获取Session变量(当前 项目有效)
Session::getLocal('name');

Copy after login

这样,和全局的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`)
)

Copy after login

注意,Db Session方式的数据库连接会采用项目的数据库配置信息进行连接。除了数据库方式外,还可以增加其它方式的Session保存机制,例如内存方式、 Memcache方式等,我们只要增加相应的过滤器就行了,使用session_set_save_handler 方法,具体的方法定义参考Think.Util.Filter下面的FilterSessionDb.class.php 文件的实现。

制作了一个简单的登陆判断
登陆检测之后赋予Session值,使Session的值为非空即为假的false

$_SESSION[C('USER_AUTH_KEY')] = $logInFind['id'] ;

Copy after login

其中 [C('USER_AUTH_KEY')]为ThinkPHP的内置方法和函数类。在未配置config.php文件时默认为空
把$logInFind['id'] 取出的帐号值赋予它,默认为关闭页面Session就自动删除消失!
其它页面使用下面格式判断

if(!isset($_SESSION[C('USER_AUTH_KEY')])) { //isset 是检测变量是否赋值!
   $this->redirect('Login','Login'); //转到注册页面
}

Copy after login
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

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 usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

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

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

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

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

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.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

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.

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

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

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

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.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

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.

See all articles