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 '))