How to let thinkphp in the model automatically completes the session assignment small tutorial _php skill

Source: Internet
Author: User
Tags session id sessions

Believe that users who have used thinkphp know that thinkphp models can perform many auxiliary functions, such as automatic verification, automatic completion, etc., today in the development of the automatic completion of the need to get session value

Then automatically assign value to the function, 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 protected function
  GetName () {return
    $_session[' name '}
} in the session


Here you need 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 the callback method defined in the current model

Session for session setup, get, delete, and manage actions
Usage Session ($name, $value = ')
Parameters Name (required): If an incoming array indicates session initialization, if passing in null indicates emptying the current session, a string represents the session assignment, fetch, or operation.
Value (optional): The session value to set, if passing in NULL indicates a session deletion, the default is an empty string
return value See details (return different values according to the specific usage)

The session function is a multiplication function, in which different parameters can be invoked to perform different function operations, including the following functions. [-more-]
Session Initialization settings
If the name parameter of the session method is passed in an array, the session initialization setting is represented, for example:
Session (Array (' name ' => ' session_id ', ' expire ' =>3600));

The incoming session parameters are supported including:


Name of parameter Description
Id session_id value
Name Session_name value
Path Session_save_path value
Prefix Session Localized space prefix
Expire Session.gc_maxlifetime Set Value
Domain Session.cookie_domain Set Value
Use_cookies Session.use_cookies Set Value
Use_trans_sid Session.use_trans_sid Set Value
Cache_limiter Session_cache_limiter Set Value
Cache_expire Session_cache_expire Set Value
Type Session hander type, you can use the hander Drive extension

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

By default, the system starts the session automatically after initialization, and you can set Session_auto_start to False if you do not want the system to start the session automatically, for example:

' Session_auto_start ' =>false

Turn off the public files that can be project after automatic startup or start the session in the Controller by manually calling Session_Start or session (' [Start] ').
Session Assignment
Session assignment is relatively simple, direct use:

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

Equivalent:

$_session[' name ' = ' value ';

Session Take value

Session value using:
$value = Session (' name ');

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

Session deletion

Session (' name ', null); Delete Name is

equivalent to:
unset ($_session[' name '));

To delete all sessions, you can use: Session
(NULL);//clear Current session

equivalent:
$_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, using the following:
Session (' [Operation name] ');

The supported operation names include:

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

Use the example below:
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 session settings, or if you set the Session_prefix parameter individually. When you start a localized session, all assignment, value, delete, and judgment operations automatically support localized sessions.

When localized session support is turned on, the generated session data format is formatted from the original
$_session[' name ' becomes $_session[' prefix ' [' name ']

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

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

Value-taking operation:
$value = Session (' name ');

Equivalent to use:
$value = $_session[' It ' [' name '];

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

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

Empty operation:
Session (NULL);

Equivalent:
unset ($_session[') ";

Judgment action:
Session ('? name ');

Equivalent:
isset ($_session[', ' 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.