How to Write logon code for thinkphp

Source: Internet
Author: User

Purpose: After successful registration, go to the homepage and tell you that you are a logon user.
 
Step 1: Add the following in the config. php file:
'User _ auth_key '=> 'authid'
Example:
<? PHP
If (! Defined ('Think _ path') Exit ();
Return array (
// Define database connection information
'Db _ type' => 'mysql', // specifies that the database is MySQL.
'Db _ host' => 'localhost ',
'Db _ name' => 'myuser', // Database Name
'Db _ user' => 'root ',
'Db _ pwd' => '', // your database connection password
'Db _ port' => '123 ',
'Db _ prefix' => 'think _ ', // data table prefix
'User _ auth_key '=> 'authid'
);
?>
Step 2: Use session: Set (C ('user _ auth_key '), $ user) in the insert () Code of adminaction. Class. php );
Save the logon username to the session. The complete code is as follows,
Public Function insert ()
{
Header ('content-type: text/html; charset = UTF-8 '); // prevents garbled characters
$ User = $ _ post ['user'];
$ This-> verifycheck ();
$ Pagemodel = D ("user ");
$ Vo = $ pagemodel-> Create ();
If (false ===$ VO) Die ($ pagemodel-> geterror ());
$ Topicid = $ pagemodel-> Add (); // The add method returns the primary key value of the newly added record.
If ($ topicid)
{
// $ _ Session [C ('user _ auth_key ')] = $ user; // This sentence cannot be used.
Session: Set (C ('user _ auth_key '), $ user );
// Dump (Session: Get ('authid '));
Echo "<SCRIPT> alert ('database added successfully'); location. href = 'HTTP: // 127.0.0.1: 3000/zhuce/index. PHP/Index'; </SCRIPT> ";
}
Else throw_exception ("<SCRIPT> alert ('database addition failed'); history. Back (); </SCRIPT> ");
}
Step 3: Use if (! Session: is_set (C ('user _ auth_key ') determines if the user has logged on. Session: Get (C ('user _ auth_key ') is the name of the login user. The Code is as follows:
Public Function Index ()
{
If (! Session: is_set (C ('user _ auth_key ')))
// If (! Isset ($ _ session ['user _ auth_key ']) | ($ _ session ['user _ auth_key'] = 0) // This sentence cannot be used
{
$ MSG = "the user has not logged on ";
}
Else
{
$ MSG = session: Get (C ('user _ auth_key ').' Welcome back ';
}
$ This-> assign ('msg ', $ MSG );
$ This-> display ();
}
 
Step 4: display the template on the homepage. The Code is as follows:
<Body>
{$ MSG} <br/>
This is my homepage
</Body>
 
Conclusion: The logon code is centered on writing sessions, judging sessions, and reading sessions.
Write session: Session: Set (C ('user _ auth_key '), $ user );
Used to determine the session: If (! Session: is_set (C ('user _ auth_key ')));
Read session: Session: Get (C ('user _ auth_key '))

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.