Table of Contents
如何让thinkphp在模型中自动完成session赋值小教程,thinkphpsession
我的thinkphp用的31版本,但是自动验证,字段映射,自动完成按照官方步骤做的都不可以用
php中 用的是thinkphp模板 怎让用户保持在登录状态用session
Home php教程 php手册 如何让thinkphp在模型中自动完成session赋值小教程,thinkphpsession

如何让thinkphp在模型中自动完成session赋值小教程,thinkphpsession

Jun 13, 2016 am 09:25 AM
session thinkphp Assignment

如何让thinkphp在模型中自动完成session赋值小教程,thinkphpsession

相信用过thinkphp的用户都知道thinkphp的模型可以完成很多辅助功能,比如自动验证、自动完成等,今天在开发中遇到自动完成中需要获取session值

然后自动赋值的功能,具体看代码;

class ArticlelModel extends Model {
  
  protected $_auto = array ( 
    array('addtime','time',1,'function'),
    array('username','getName',1,'callback')
  );
  
  //这个函数获取session里的name值
  protected function getName(){
    return $_SESSION["name"];
  }
}
Copy after login


这里需要注意最后一个参数function和callback的区别;
function:使用函数,会自动去Common/common.php去寻找对应的函数;
callback:使用在当前模型中定义的回调方法

Session 用于Session 设置、获取、删除和管理操作
用法 session($name, $value='')
参数 name(必须):如果传入数组 则表示进行session初始化,如果传入null表示清空当前session,如果是字符串则表示session赋值、获取或者操作。
Value(可选):要设置的session值,如果传入null表示删除session,默认为空字符串
返回值 见详(根据具体的用法返回不同的值)

session函数是一个多元化操作函数,传入不同的参数调用可以完成不同的功能操作,包括下面一些功能。[-more-]
session初始化设置
如果session方法的name参数传入数组则表示进行session初始化设置,例如:
session(array('name'=>'session_id','expire'=>3600));

支持传入的session参数包括:


参数名 说明
id session_id值
name session_name 值
path session_save_path 值
prefix session 本地化空间前缀
expire session.gc_maxlifetime 设置值
domain session.cookie_domain 设置值
use_cookies session.use_cookies 设置值
use_trans_sid session.use_trans_sid 设置值
cache_limiter session_cache_limiter设置值
cache_expire session_cache_expire设置值
type session hander类型,可以使用hander驱动扩展

Session初始化设置方法 无需手动调用,在App类的初始化工作结束后会自动调用,通常项目只需要配置SESSION_OPTIONS参数即可,SESSION_OPTIONS参数的设置是一个数组,支持的索引名和前面的session初始化参数相同。

默认情况下,初始化之后系统会自动启动session,如果不希望系统自动启动session的话,可以设置SESSION_AUTO_START为false,例如:

'SESSION_AUTO_START' =>false
Copy after login

关闭自动启动后可以项目的公共文件或者在控制器中通过手动调用session_start或者session('[start]') 启动session。
session赋值
Session赋值比较简单,直接使用:

session('name','value'); //设置session
Copy after login

相当于:

$_SESSION['name'] = 'value';
Copy after login

session取值

Session取值使用:
$value = session('name');

相当于使用:
$value = $_SESSION['name'];

Copy after login

session删除

session('name',null); // 删除name

相当于:
unset($_SESSION['name']);

要删除所有的session,可以使用:
session(null); // 清空当前的session

相当于:
$_SESSION = array();
Copy after login

session判断
要判断一个session值是否已经设置,可以使用
session('?name');

用于判断名称为name的session值是否已经设置
相当于:
isset($_SESSION['name']);

session管理
session方法支持一些简单的session管理操作,用法如下:
session('[操作名]');

支持的操作名包括:

操作名 含义
start 启动session
pause 暂停session写入
destroy 销毁session
regenerate 重新生成session id

使用示例如下:
session('[pause]'); // 暂停session写入
session('[start]'); // 启动session
session('[destroy]'); // 销毁session
session('[regenerate]'); // 重新生成session id

本地化支持

如果在初始化session设置的时候传入prefix参数或者单独设置了SESSION_PREFIX参数的话,就可以启用本地化session管理支持。启动本地化session后,所有的赋值、取值、删除以及判断操作都会自动支持本地化session。

本地化session支持开启后,生成的session数据格式由原来的
$_SESSION['name'] 变成 $_SESSION['前缀']['name']

假设前缀设置为think,则赋值操作:
session('name','value');  //设置session

相当于:
$_SESSION['think']['name'] = 'value';

取值操作:
$value = session('name');

相当于使用:
$value = $_SESSION['think']['name'];

删除操作:
session('name',null);

相当于:
unset($_SESSION['think']['name']);

清空操作:
session(null);

相当于:
unset($_SESSION['think']);

判断操作:
session('?name');

相当于:
isset($_SESSION['think']['name']);

我的thinkphp用的31版本,但是自动验证,字段映射,自动完成按照官方步骤做的都不可以用

$User=new Model('Admin'); // 问题在这一句代码。Model()或M()方法实例化基础模型,而你在模型中添加了自动验证,属于自定义模型。因此自动验证部分不会又作用。// 将上一句按如下修改$User=new AdminModel(); // 直接实例化为你的自定义模型// 或者更简单的$User=D('Admin');// 详细内容参考官方文档 6.2 模型实例化
 

php中 用的是thinkphp模板 怎让用户保持在登录状态用session

thinkphp中是通过在项目文件夹下的conf文件夹下面的config.php重新对session进行“增,删,改,查”配置的,首先找到相对的部分然后看使用哪种方法进行cookie的,假如是mencache服务器上保存的那还有mencache的配置方法,实现的方式很多,具体的要看过才知道哦
 

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

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)

How to enable copy and paste for VMware virtual machines How to enable copy and paste for VMware virtual machines Feb 21, 2024 am 10:09 AM

You can easily copy and paste text and files between VMware virtual machines (VMs) and physical systems. This capability allows you to easily transfer images, formatted and unformatted text, and even email attachments between virtual machines and host systems. This article will show you how to enable this feature and demonstrate methods for copying data, files, and folders. How to Enable Copy/Paste in VMware VMware provides three different ways to copy data, files or folders from a virtual machine to a physical computer and vice versa, as explained below: Copy and Paste Elements Drag and Drop Feature Folder Sharing 1 ] Enable copy-paste using VMware Tools You can use the keyboard if your VMWare installation and guest operating system meet the requirements

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

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.

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.

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

See all articles