How to get thinkphp to automatically complete the session assignment in the Model tutorial, thinkphpsession_php Tutorial

Source: Internet
Author: User
Tags session id

How to get thinkphp to automatically complete the session assignment in the Model tutorial, Thinkphpsession


I believe that users who have used thinkphp know that the thinkphp model can accomplish many auxiliary functions, such as automatic verification, auto-completion, and so on, we need to get session value in the auto-completion in development today.

Then automatically assign the function of the value, the specific look at 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"];}  }


It is important to note the difference between the last parameter function and the callback.
function: Using functions, will automatically go to common/common.php to find the corresponding function;
Callback: Using a callback method defined in the current model

Session for session settings, get, delete, and manage operations
Usage Session ($name, $value = ")
parameter name (required): If an array is passed in, the session initialization is performed, if NULL is passed in to empty the current session, or if it is a string, the session is assigned, fetched, or manipulated.
value (optional): The session value to be set, if the null is passed in to delete the session, the default is an empty string
return value See details (return different values depending on the specific usage)

The session function is a diversified operation function, which can accomplish different functions by passing in different parameter calls, including some of the following functions. [-more-]
Session Initialization settings
If the name parameter of the session method is passed in the array, the session initialization setting is used, for example:
Session (Array (' name ' = ' session_id ', ' expire ' =>3600));

Support for incoming session parameters includes:


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 Setting value
cache_expire session_cache_expire Setting value
Type Session hander type, can be extended with hander driver

SESSION initialization setting method does not need to call manually, after the initialization of the app class is called automatically, usually the project only needs to configure the Session_options parameter, the Session_options parameter is set to an array, The supported index names are the same as the previous session initialization parameters.

By default, the system will automatically start 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

Turn off the session by manually invoking Session_Start or session (' [Start] ') on the project's public file or in the controller after auto-start.
Session Assignment
The session assignment is relatively straightforward and is used directly:

Session (' name ', ' value '); Set session

Equivalent:

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

Session Fetch Value

Session value used: $value = Session (' name '), equivalent to use: $value = $_session[' name '];

Session Delete

Session (' name ', null); Delete Name equals: unset ($_session[' name '); To delete all sessions, you can use: session (NULL); Emptying the current SESSION is equivalent to: $_session = Array ();

Session Judgment
To determine if a session value has been set, you can use the
Session ('? name ');

Used to determine if the session value named name is already set
Equivalent:
Isset ($_session[' name ');

Session Management
The session method supports some simple session management operations with the following usage:
Session (' [Operation name] ');

The supported operation names are:

Action name meaning
Start Start session
Pause Pause Session Write
Destroy Destroy session
Regenerate Regenerate session ID

Examples of use are:
session (' [Pause] ');//Pause Session Write
Session (' [Start] '); Start session
Session (' [Destroy] '); Destroy session
Session (' [Regenerate] '); Regenerate session ID

Localization support

Localized session management support can be enabled if you pass in the prefix parameter when initializing the session setting or if the Session_prefix parameter is set individually. After the localization session is started, all assignments, values, deletions, and judgments will automatically support localization sessions.

After localization session support is turned on, the session data format generated by the original
$_session[' name '] becomes $_session[' prefix ' [' name ']

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

Equivalent:
$_session[' think ' [' name '] = ' value ';

To take a value action:
$value = Session (' name ');

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

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

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

Clear operation:
session (NULL);

Equivalent:
unset ($_session[' think ');

To determine the operation:
Session ('? name ');

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


My thinkphp uses the 31 version, but automatic validation, field mapping, auto-completion according to the official steps can not be used

$User =new Model (' Admin '); Question in this sentence code. The model () or M () method instantiates the underlying model, and you add automatic validation to the models, which belongs to the custom model. So the auto-validation section doesn't work. Revise the previous sentence as follows $user=new Adminmodel (); Directly instantiate to your custom model//or simpler $user=d (' Admin ');//Detail Reference Official Document 6.2 model instantiation

PHP is used in the thinkphp template how to keep the user in the login state with the session

Thinkphp in the project folder under the Conf folder under the config.php re-"add, delete, change, check" configuration, first find the relative part and then see which method to use cookies, If the Mencache server is saved on that there are Mencache configuration method, the way to achieve a lot, specific to see before you know OH

http://www.bkjia.com/PHPjc/874631.html www.bkjia.com true http://www.bkjia.com/PHPjc/874631.html techarticle How to let thinkphp in the model automatically complete the session Assignment small tutorial, thinkphpsession believe that thinkphp users know thinkphp model can do a lot of auxiliary functions, such as automatic ...

  • 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.