How to make thinkphp automatically complete the session assignment in the model? tutorial _ PHP

Source: Internet
Author: User
When a problem occurs in the project, thinkphp needs to automatically assign values to Sessions in the model. after some research, thinkPHP is finally implemented. The following is a record, and a ThinkPHP session-related knowledge is attached.

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']);

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.