Implementation of ThinkPHP login function, thinkphp login implementation _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP login implementation method, thinkphp login implementation. ThinkPHP login implementation method, thinkphp login implementation login function is a common function in PHP programming. In this article, the ThinkPHP instance enters the home page after successful registration and tells you how to implement the ThinkPHP login function. thinkphp login implementation

Login is a common function in PHP programming. The ThinkPHP instance in this article mainly enters the home page after successful registration and tells you that it is a login user function. The specific implementation steps are as follows:

Step 1: Add the following to the config. php file:

'USER_AUTH_KEY'=>'authId'

Example:

<? Phpif (! Defined ('think _ path') exit (); return array (// defines the database connection information 'DB _ type' => 'mysql ', // The specified 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 the insert () code in AdminAction. class. php:

Session::set(C('USER_AUTH_KEY'),$user);

Save the logon username to the session.

The complete implementation 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; // The Session: set (C ('User _ AUTH_KEY') cannot be used '), $ user); // dump (Session: get ('authid'); echo "script" alert ('database added successfully'); location. href = 'http: // 127.0.0.1/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 )) // you cannot use this {$ 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:

{$ Msg}
This is my homepage

Summary:

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

We hope that the implementation of the ThinkPHP login function described in this article can help you in ThinkPHP programming.


How can I use Thinkphp to develop login permissions?

If you have built-in rbac permission processing, the author will study it and it is very powerful.
 

How can I use thinkphp and js to achieve automatic webpage logon?

No js is required. when you log on, save the user information in the cookie. Then, open the page and check whether the user information in the cookie exists.

Login login is a common function in PHP programming. In this article, the ThinkPHP instance enters the home page after successful registration and tells you...

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.