This example describes the session usage in thinkphp. Share to everyone for your reference. Specifically as follows:
The session class is packaged in thinkphp and can be used directly by the user, with the following common methods:
Session::set (name, value): Register session.
Session::is_set (name): Checks whether the value of the session is set.
Session::get (name): Read session.
Session::clear (): Empty session.
Session::d Estroy (): Destroy session.
Thinkphp opens session sessions by default, so you do not need to use the session_start () function to open sessions before you use them.
Using the session instance
The following registers the session as a form submission and reads the session value in Session::get mode and in the other two pages, respectively, in templates and operations.
Register session
User Module Sessiontest Operation Registration Session Example:
Copy Code code as follows:
Class Useraction extends action{
Public Function session () {
if (!emptyempty ($_post[' username ')) {
Session::set (' username ', $_post[' username '));
}
$this->display ();
}
}
sessiontest.html Template (Fragment):
Copy Code code as follows:
<p>
<present name= "_session[' username ']" >{$_session[' username ']} Hello!
<a href= "__app__/" > Home </a>
<a href= "__url__/user" > Other pages of this module </a>
<a href= "__url__/logout" > Logoff </a><else/> Please enter your username:
</p>
<form action= "__self__" method= "POST" >
<p><input type= "text" name= "username"/></p>
<p><input type= "Submit" value= "submitted"/></p>
</form>
</present>
When filling in the user name (such as TestUser), click the Submit button, submit to the Sessiontest method (that is, the current page) process and register the session value, template tag present to control the output logic, detection has been registered $_session[' username ' Variable is the output:
TestUser Hello! Home Logout
Otherwise, the form is output.
Detect if session is registered
Test in Template
In the template, you can directly use present or notempty or even switch and other labels to determine whether the session variable registration, to determine the output of the corresponding session value (directly in the output array unit output session variable value) or display other page elements, specific to See the template label section of this tutorial and the example above.
In-operation detection
In action, you can use the Session::is_set method to check that the value of the session is set, for example, the User module user action is as follows:
Copy Code code as follows:
Public Function User () {
As the direct output 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 session's code fragment:
Copy Code code as follows:
<present name= "_session[' username ']" >{$_session[' username ']} Hello! <else/> Not logged in </present>
Session invalid (cannot be passed)
There may be cases where the session is invalid (cannot be delivered to another page) in thinkphp, as follows:
Use the session class header letter not capitalized, such as: Session::set.
The page has information output, such as a blank line for the entry file.
The session storage path (Session.save_path) permissions on the server (Linux/unix) are incorrect, resulting in the inability to properly store sessions information.
Scope issues suggest:
Thinkphp's session class is just a simple package to the session, you can actually use the PHP native session function directly in the operation, the official is so suggested.
I hope this article will be helpful to everyone's thinkphp framework program design.