How to enable thinkphp to automatically assign values to sessions in the model? thinkphpsession

Source: Internet
Author: User

How to enable thinkphp to automatically assign values to sessions in the model? thinkphpsession

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

Then the function of automatic assignment is shown in the code;

Class ArticlelModel extends Model {protected $ _ auto = array ('addtime', 'time', 1, 'function'), array ('username', 'getname ', 1, 'callback'); // This function gets the name value in the session protected function getName () {return $ _ SESSION ["name"];}


Note the difference between the function and callback of the last parameter;
Function: When a function is used, Common/common. php is automatically used to find the corresponding function;
Callback: Use the callback method defined in the current model

Sessions are used to set, obtain, delete, and manage sessions.
Usage Session ($ name, $ value = '')
Parameters Name (required): If an array is input, session Initialization is performed. If null is input, the current session is cleared. If it is a string, the session value is assigned, obtained, or operated.
Value (optional): The session Value to be set. If null is input, the session is deleted. The default Value is a null string.
Return Value See details (return different values based on specific usage)

The session function is a diverse operation function. You can call different parameters to complete different function operations, including the following functions. [-More-]
Session Initialization settings
If the name parameter of the session method is passed in an array, it indicates that the session Initialization is set. For example:
Session (array ('name' => 'session _ id', 'expire '=> 3600 ));

The following session parameters are supported:


Parameter Name Description
Id Session_id Value
Name Session_name Value
Path Session_save_path Value
Prefix Session localized space prefix
Expire Session. gc_maxlifetime Setting Value
Domain Session. cookie_domain Setting Value
Use_cookies Session. use_cookies Setting Value
Use_trans_sid Session. use_trans_sid Setting Value
Cache_limiter Session_cache_limiter
Cache_expire Session_cache_expire
Type Session hander type, which can be extended using the hander driver

The Session Initialization setting method does not need to be manually called. It is automatically called after the initialization of the App class ends. Generally, the project only needs to configure the SESSION_OPTIONS parameter. The SESSION_OPTIONS parameter is set as an array, the index names supported are the same as the previous session initialization parameters.

By default, the system automatically starts the session after initialization. If you do not want the system to automatically start the session, you can set SESSION_AUTO_START to false. For example:

'SESSION_AUTO_START' =>false

After automatic start is disabled, you can start the session by manually calling session_start or session ('[start]') in the public file of the project or controller.
Session assignment
Session assignment is relatively simple and can be used directly:

Session ('name', 'value'); // sets the session

Equivalent:

$_SESSION['name'] = 'value';

Session Value

Session value: $ value = session ('name'); equivalent to: $ value = $ _ SESSION ['name'];

Delete session

Session ('name', null); // Delete name is equivalent to: unset ($ _ SESSION ['name']); to delete all sessions, you can use: session (null); // clearing the current session is equivalent to: $ _ SESSION = array ();

Session judgment
To determine whether a session value has been set, you can use
Session ('? Name ');

Used to determine whether the session value named name has been set
Equivalent:
Isset ($ _ SESSION ['name']);

Session management
The session method supports some simple session management operations. The usage is as follows:
Session ('[operation name]');

Supported operation names include:

Operation name Description
Start Start session
Pause Pause session writing
Destroy Destroy session
Regenerate Regenerate session id

Example:
Session ('[pause]'); // pause session writing
Session ('[start]'); // start the session
Session ('[destroy]'); // destroy the session
Session ('[regenerate]'); // regenerate the session id

Localization support

If the prefix parameter is input during session Initialization or the SESSION_PREFIX parameter is set separately, the local session management support can be enabled. After a localized session is started, all assignment, value, deletion, and judgment operations automatically support the localized session.

After the local session is enabled, the format of the generated session data is changed from
$ _ SESSION ['name'] becomes $ _ SESSION ['prefix'] ['name']

If the prefix is set to think, the value assignment operation is as follows:
Session ('name', 'value'); // sets the session

Equivalent:
$ _ SESSION ['think'] ['name'] = 'value ';

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

Equivalent:
$ Value = $ _ SESSION ['think'] ['name'];

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

Equivalent:
Unset ($ _ SESSION ['think'] ['name']);

Clear operation:
Session (null );

Equivalent:
Unset ($ _ SESSION ['think']);

Judgment operation:
Session ('? Name ');

Equivalent:
Isset ($ _ SESSION ['think'] ['name']);


My thinkphp version 31 is used, but it cannot be used for automatic verification and field ing.

$ User = new Model ('admin'); // the problem lies in this Code. The Model () or M () method instantiates the basic Model, and you add automatic verification to the Model, which belongs to the custom Model. Therefore, automatic verification does not work. // Modify the previous sentence as follows: $ User = new AdminModel (); // directly instantiate your custom model // or simpler $ User = D ('admin'); // For details, refer to official document 6.2 model instantiation.

The thinkphp template is used in php. How can users keep the session in the logon status?

In thinkphp, config is created under the conf folder in the project folder. php re-adds, deletes, modifies, and queries sessions. First, it finds the corresponding part and then checks which method is used for cookie, if it is stored on the mencache server, there are also the mencache configuration methods, there are many implementation methods, the specific needs have to be viewed to know oh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.