How to make thinkphp automatically complete session assignment tutorial in the model
- 2021-07-18 07:24:54
- OfStack
I believe that users who have used thinkphp know that the model of thinkphp can complete many auxiliary functions, such as automatic verification, automatic completion, etc. Today, when encountering automatic completion in development, we need to obtain session value
Then the function of automatic assignment, see the code specifically;
class ArticlelModel extends Model {
protected $_auto = array (
array('addtime','time',1,'function'),
array('username','getName',1,'callback')
);
// This function gets the session In name Value
protected function getName(){
return $_SESSION["name"];
}
}
Here, we should pay attention to the difference between the last parameter function and callback.
function: If you use a function, you will automatically go to Common/common. php to find the corresponding function;
callback: Use callback methods defined in the current model
Session 用于Session 设置、获取、删除和管理操作 | |
---|---|
用法 | session($name, $value='') |
参数 | name(必须):如果传入数组 则表示进行session初始化,如果传入null表示清空当前session,如果是字符串则表示session赋值、获取或者操作。 Value(可选):要设置的session值,如果传入null表示删除session,默认为空字符串 |
返回值 | 见详(根据具体的用法返回不同的值) |
session function is a diversified operation function, which can complete different functional operations by calling different parameters, including the following 1 functions. [-more-]
session initialization settings
If the name parameter of the session method is passed into the array, it means that session initialization settings are made, for example:
session(array('name'=
>
'session_id','expire'=
>
3600));
The session parameters that support passing in include:
参数名 | 说明 |
---|---|
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驱动扩展 |
The Session initialization setting method does not need to be called manually, but will be called automatically after the initialization of App class. Usually, the project only needs to configure SESSION_OPTIONS parameter, and the setting of SESSION_OPTIONS parameter is an array, and the supported index name is the same as the previous session initialization parameter.
By default, the system will automatically start session after initialization. If you don't want the system to start session automatically, you can set SESSION_AUTO_START to false, for example:
'SESSION_AUTO_START' =>false
After auto-start is turned off, session can be started in the public file of the project or in the controller by manually calling session_start or session ('[start]').
session assignment
Session assignment is relatively simple, and you can use it directly:
session('name','value'); // Settings session
Equivalent to:
$_SESSION['name'] = 'value';
session Value
Session Value use:
$value = session('name');
Equivalent to using:
$value = $_SESSION['name'];
session Delete
session('name',null); // Delete name
Equivalent to:
unset($_SESSION['name']);
To delete all session You can use:
session(null); // Empty the current session
Equivalent to:
$_SESSION = array();
session judgment
To determine whether 1 session value has been set, you can use
session('?name');
Used to determine whether the session value named name has been set
Equivalent to:
isset($_SESSION['name']);
session Management
The session method supports a number of simple session management operations, which are used as follows:
session ('[Operation Name]');
Supported operation names include:
操作名 | 含义 |
---|---|
start | 启动session |
pause | 暂停session写入 |
destroy | 销毁session |
regenerate | 重新生成session id |
Examples of use are as follows:
session ('[pause]'); //Pause session writes
session ('[start]'); //Start session
session ('[destroy]'); //Destroy session
session ('[regenerate]'); //Rebuild session id
Localization support
Localized session management support can be enabled if the prefix parameter is passed during initialization of session settings or if the SESSION_PREFIX parameter is set separately. When Localized session is started, all assignments, values, deletions, and judgments automatically support Localized session.
When localized session support is turned on, the generated session data format is 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';
Valuation operation:
$value = session('name');
Equivalent to using:
$value = $_SESSION['think']['name'];
Delete operation:
session('name',null);
Equivalent to:
unset($_SESSION['think']['name']);
Empty operation:
session(null);
Equivalent to:
unset($_SESSION['think']);
Judgment action:
session('?name');
Equivalent to:
isset($_SESSION['think']['name']);