search
HomePHP FrameworkThinkPHPHow to realize registration and login in ThinkPHP6 through Ucenter

The following tutorial column will introduce you to the registration and login method of ThinkPHP6 through Ucenter. I hope it will be helpful to friends in need! ThinkPHP6 realizes registration and login through Ucenter

After successful communication with ucenter, it is very simple to realize registration and login. A brief record follows.

1 Create a new controller to inherit Ucenter\Controller\UcController, and create a new constructor method to instantiate UcController
<?php declare (strict_types = 1);

namespace app\controller;

use Ucenter\Controller\UcController;
class uc extends UcController
{
    /**
     * 构造方法,用于实例化 UcController
     */
    public function __construct(){
        $this->init();
    }
}

2 Index controller adds uc class variables.

private $uc;


3 Instantiate the uc controller in the index method, and operate the UCenter interface through $this->uc. $this->uc = new uc();


After the above two steps are completed, you can call the ucenter interface in the appropriate place in the code to register and log in. Below is a code snippet from my project for reference only. 4 When registering, verify whether the username already exists and is legal

                $username = $data['Content'];
                $result = $this->uc->uc_check_name($username);
                switch ($result) {
                    case -1:
                        $reason = "用户名不合法";
                        break;
                    case -2:
                        $reason = "包含不允许注册的词语";
                        break;
                    case -3:
                        $reason = "用户名已经存在";
                        break;
                }
                if ($result != 1) {
                    $this->_resetStep();
                    return array(join("\n", array_merge(array('【注册】注册失败', $reason), $this->_guestActions()
                    )),
                        'text');
                } else {
                    $this->_setStep(self::STEP_REGISTER_PASSWORD);
                    Session::set('username', $data['Content']);

                    return array('【注册】请输入密码', 'text');
                }
            }

5 After entering the password, call ucenter to register. Because ucenter must bring username, password, and email when registering, it needs to be the user Generate the default email

            //注册->输入密码
            if ($this->_currentStep() == self::STEP_REGISTER_PASSWORD) {
                $this->_resetStep();
                Session::set('password', $data['Content']);
                //call ucenter to register user
                $username_valid = $this->uc->uc_check_name(Session::get('username'));
                Log::write('$username_valid' . $username_valid, 'debug');
                if ($username_valid == 1) {
                    //register
                    Log::write('start register ' . Session::get('username'), 'debug');
                    $email = "reg_" . substr(Session::getId(), 0, 3) . time() . substr(Session::getId(), 7, 4) .
                        "@null.null";
                    //$email = Session::get('username').'@'.Session::get('username').'.com';
                    $register_result = $this->uc->uc_register(Session::get('username'), Session::get('password'),
                        $email);
                    Log::write('register result is ' . $register_result, 'debug');
                    switch ($register_result) {
                        case -1:
                            $reason = "用户名不合法";
                            break;
                        case -2:
                            $reason = "包含不允许注册的词语";
                            break;
                        case -3:
                            $reason = "用户名已经存在";
                            break;
                        case -4:
                            $reason = "Email格式有误";
                            break;
                        case -5:
                            $reason = "Email不允许注册";
                            break;
                        case -6:
                            $reason = "该Email已经被注册";
                            break;
                    }
                    if ($register_result > 0) {
                        Log::write('Ucenter register successful' . Session::get('username'), 'debug');
                        return array(join("\n", array_merge(array('【注册】注册成功'), $this->_guestActions())), 'text');
                    } else {
                        Log::write('Ucenter register failed' . $reason, 'debug');
                        $this->_resetSession();
                        return array(join("\n", array_merge(array('【注册】注册失败', $reason), $this->_guestActions()
                        )),
                            'text');
                    }
                }

            }

6. Just pass in the username and password entered by the user when logging in. After successful login, write the username and password into the session file.

$result = $this->uc->uc_login(Session::get('username'), $data['Content']);

Related recommendations:

The latest 10 thinkphp video tutorials

The above is the detailed content of How to realize registration and login in ThinkPHP6 through Ucenter. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software