The example in this paper describes the session usage in thinkphp. Share to everyone for your reference. Specific as follows:
In the thinkphp package the session class, the user can directly use, the commonly used methods are:
Session::set (name, value): registers the session.
Session::is_set (name): Check if the value of session is set.
Session::get (name): reads session.
Session::clear (): Clears the session.
Session::d Estroy (): Destroys session.
The thinkphp session is turned on by default, so you do not need to use the session_start () function to open the session before using the session class.
Using Session instances
The following is a form submitted to register the session, and in the other two pages in the template mode and operation of the Session::get to read the session value.
Register session
User Module Sessiontest Operation Registration Session Example:
The code is as follows:
Class Useraction extends action{ Public Function session () { if (!emptyempty ($_post[' username ')) { Session::set (' username ', $_post[' username '); } $this->display (); } }
sessiontest.html Template (Fragment):
The code is as follows:
{$_session[' username '} Hello! Home Other pages of this module Log out Please enter your user name:
When the user name (such as TestUser), click the Submit button, submit to the Sessiontest method (i.e. the current page) processing and registration session value, with the template tag present to control the output logic, detected registered $_session[' username ' ] variable is output:
TestUser Hello! Home Logout
Otherwise, the form is output.
Detect if session is registered
Detection in templates
In the template, you can directly use present or notempty even switch and other tags to determine whether the session variable registration, to determine the output of the corresponding session value (directly output array unit output session variable value) or display other page elements, specifically to See the section on the Template tab of this tutorial and the example above.
In-operation detection
In the operation, you can use the Session::is_set method to check whether the value of the Session is set, such as the user module user operation as follows:
The code is as follows:
Public Function User () { Because the output is directly in the operation, in order to avoid garbled Header ("content-type:text/html; Charset=utf-8 "); if (Session::is_set (' username ')) { echo session::get (' username '). ' Hello '; }else{ Echo ' Session not registered '; } }
Other module pages
In other pages (such as Index/index), judge and read the snippet of the session:
The code is as follows:
{$_session[' username '} Hello! not logged in
Invalid session (cannot be delivered)
There may be a case in thinkphp where the session is invalid (cannot be delivered to another page), possibly for the following reasons:
Use the Session class with a header letter that is not capitalized, such as: Session::set.
The page has information output, such as the entry file has a blank line.
The session storage path (Session.save_path) permissions on the server (Linux/unix) are incorrect, which prevents the session information from being stored correctly.
Scope Problem Recommendations:
Thinkphp's session class is simply a simple wrapper for the session, which can actually be used to directly use PHP's native session function in the operation, which is also recommended by the authorities.
It is hoped that this article will be helpful to everyone's thinkphp framework design.
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.