Session usage in ThinkPHP _ php instance

Source: Internet
Author: User
This article mainly introduces the Session usage in ThinkPHP, and analyzes the common session operation skills in ThinkPHP and the handling methods of invalid sessions in the form of examples, thinkPHP is a very practical technique for project development. if you need ThinkPHP, you can refer to the example in this article to describe the Session usage in ThinkPHP. Share it with you for your reference. The details are as follows:

The Session class is encapsulated in ThinkPHP and can be directly used by users. Common methods include:

Session: set (name, value): registers a session.

Session: is_set (name): Check whether the Session value is set.

Session: get (name): Read session.

Session: clear (): clear the Session.

Session: destroy (): destroys the session.

The session is enabled by default in ThinkPHP. Therefore, you do not need to use the session_start () function to enable the Session before using the session class.

Use session instances

The following describes how to register a session by submitting a form, and read the Session value by session: get on the other two pages in template mode and operation mode.

Register a session

Example of registering a session using the User module sessionTest:

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 (segment ):

The code is as follows:

{$ _ SESSION ['username']} Hello!
Other pages of this module
Cancel Enter your username:

After entering the username (such as testuser), click the submit button and submit it to the sessionTest method (that is, the current page) for processing and registering the session value, and use the template tag present to control the output logic, it is detected that the variable $ _ SESSION ['username'] has been registered and output:

Hello testuser! Homepage logout

Otherwise, the form is output.

Check whether the session is registered

Template Detection

In the template, you can directly use the present, notempty, or even switch tags to determine whether the session variable is registered, to determine the output of the corresponding session value (directly output the session variable value in the form of an output array unit) or display other page elements. for details, refer to the template tag section of this tutorial and the above example.

Operation detection

In the operation, you can use the Session: is_set method to check whether the Session value is set. for example, the User operation of the user module is as follows:

The code is as follows:

Public function user (){
// Because it is output directly in the operation, to avoid garbled characters
Header ("Content-Type: text/html; charset = utf-8 ");
If (Session: is_set ('Username ')){
Echo Session: get ('Username'). '';
} Else {
Echo 'session not registered ';

Other module pages

In other pages (such as Index/index), judge and read the session code segment:

The code is as follows:

{$ _ SESSION ['username']} Hello! Not logged on

Invalid session (unable to pass)

ThinkPHP may have an invalid session (which cannot be passed to other pages). the possible reasons are as follows:

The Session class header letters are not capitalized, for example, session: set.

The page has information output, such as empty rows in the entry file.

The session storage path (session. save_path) permission on the server (Linux/Unix) is incorrect, and the session information cannot be properly stored.

Suggestion on scope:

The Session class of ThinkPHP is only a simple packaging of Sessions. In practice, you can directly use the native session function of PHP in the operation. this is also officially recommended.

I hope this article will help you with ThinkPHP framework programming.

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