Home > Backend Development > PHP Tutorial > A small tutorial on how to let thinkphp automatically complete session assignment in the model, thinkphpsession_PHP tutorial

A small tutorial on how to let thinkphp automatically complete session assignment in the model, thinkphpsession_PHP tutorial

WBOY
Release: 2016-07-13 10:19:27
Original
914 people have browsed it

A small tutorial on how to let thinkphp automatically complete session assignment in the model, thinkphpsession

I believe that users who have used thinkphp know that thinkphp’s model can complete many auxiliary functions, such as automatic verification, automatic completion, etc. Today during development, I encountered the need to obtain the session value during automatic completion

Then the function of automatic assignment, please see the code for details;

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


Here you need to pay attention to the difference between the last parameter function and callback;
function: When using a function, it will automatically go to Common/common.php to find the corresponding function;
callback: Use the callback method defined in the current model

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

The session function is a diversified operation function. Passing in different parameters can complete different functional operations, including the following functions. [-more-]
session initialization settings
If the name parameter of the session method is passed into the array, it means session initialization settings, for example:
session(array('name'=>'session_id','expire'=>3600));

Supported session parameters include:


Parameter name Description
id session_id value
name session_name value
path session_save_path value
prefix session localization space prefix
expire session.gc_maxlifetime setting value
domain session.cookie_domain setting value
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

Usage examples are as follows:
session('[pause]'); // Pause session writing
session('[start]'); // Start session
session('[destroy]'); // Destroy session
session('[regenerate]'); // Regenerate session id

Localization support

If you pass in the prefix parameter when initializing the session settings or set the SESSION_PREFIX parameter separately, you can enable localized session management support. After starting the localized session, all assignment, value acquisition, deletion and judgment operations will automatically support the localized session.

After localized session support is turned on, the generated session data format will be changed from the original
$_SESSION['name'] becomes $_SESSION['prefix']['name']

Assuming the prefix is ​​set to think, the assignment operation:
session('name','value'); //Set session

Equivalent to:
$_SESSION['think']['name'] = 'value';

Value operation:
$value = session('name');

is equivalent to using:
$value = $_SESSION['think']['name'];

Delete operation:
session('name',null);

Equivalent to:
unset($_SESSION['think']['name']);

Clear operation:
session(null);

Equivalent to:
unset($_SESSION['think']);

Judgment operation:
session('?name');

Equivalent to:
isset($_SESSION['think']['name']);

I use version 31 of thinkphp, but automatic verification, field mapping, and automatic completion according to the official steps cannot be used

$User=new Model('Admin'); //The problem lies in this line of code. The Model() or M() method instantiates the basic model, and you add automatic validation to the model, which is a custom model. Therefore the automatic verification part will not work anymore. // Modify the previous sentence as follows $User=new AdminModel(); // Directly instantiate it as your custom model // Or simpler $User=D('Admin'); // For details, refer to the official documentation 6.2 Model instantiation

The thinkphp template is used in php. How to keep the user logged in using session

In thinkphp, the session is re-configured by "adding, deleting, modifying and checking" through config.php under the conf folder in the project folder. First find the corresponding part and then see which method to use for cookies. Yes, if it is saved on the mencache server, there is also a mencache configuration method. There are many ways to implement it. You have to read it to know the details

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/874631.htmlTechArticleA short tutorial on how to let thinkphp automatically complete session assignment in the model, thinkphpsession. I believe users who have used thinkphp know about thinkphp. The model can complete many auxiliary functions, such as automatically...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template