How to implement login in yii
How to implement login in yii
1. Create the data table shop_admin
CREATE TABLE `shop_admin` ( `adminid` int(10) UNSIGNED NOT NULL COMMENT '主键ID', `adminuser` varchar(32) NOT NULL DEFAULT '' COMMENT '管理员账号', `adminpass` char(32) NOT NULL DEFAULT '' COMMENT '管理员密码', `adminemail` varchar(50) NOT NULL DEFAULT '' COMMENT '管理员邮箱', `logintime` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '登陆时间', `loginip` bigint(20) NOT NULL DEFAULT '0' COMMENT '登陆IP', `createtime` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '创建时间' ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. Login page
<?php use yii\bootstrap\ActiveForm; use yii\helpers\Html; $form = ActiveForm::begin([ 'id' => 'abc-form', 'options' => ['class' => 'form-horizontal'], ])?> <?= $form->field($model, 'adminuser')->textInput(['placeholder' => "用户名"])->label('账号') ?> <?= $form->field($model, 'adminpass')->passwordInput()->label('密码') ?> <?= Html::submitButton('提交') ?> <?php ActiveForm::end() ?>
3. Controller
Related article tutorial recommendations: yii Tutorial
<?php namespace app\controllers; use yii\web\Controller; use app\models\Admin; use Yii; class IndexController extends Controller { public function actionIndex() { // 不使用布局 $this->layout = false; $model = new Admin; // 是否是post提交 if (Yii::$app->request->isPost) { // 获得post提交参数 $post = Yii::$app->request->post(); if($model->login($post)){ return "登陆成功"; } else { return "登陆失败"; } } else { return $this->render("index", ['model' => $model]); } } }
4, Model
<?php namespace app\models; use yii\db\ActiveRecord; use Yii; class Admin extends ActiveRecord { public static function tableName() { return "{{%admin}}"; } public function rules() { return [ ['adminuser', 'required'], ['adminpass', 'required'], // 验证密码是否正确 ['adminpass', 'validatePass'] ]; } public function validatePass() { if (!$this->hasErrors()) { // 判断用户名密码是否正确 $data = self::find() ->where(['adminuser' => $this->adminuser]) ->andwhere(['adminpass' => md5($this->adminpass)]) ->one(); if (is_null($data)) { $this->addError('adminpass', 'adminuser or adminpass error'); } } } public function login($data) { if($this->load($data) && $this->validate()) { // 登陆信息写入session $session = Yii::$app->session; $session->open(); $session->set('adminuser', $this->adminuser); // 更新登陆时间和IP $this->updateAll(['logintime' => time(), 'loginip' => ip2long(Yii::$app->request->userIP)], ['adminuser' => $this->adminuser]); return true; } return false; } }
For more yiiIntroduction to Programming tutorials, please pay attention to the PHP Chinese website.
The above is the detailed content of How to implement login in yii. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When you log in to someone else's steam account on your computer, and that other person's account happens to have wallpaper software, steam will automatically download the wallpapers subscribed to the other person's account after switching back to your own account. Users can solve this problem by turning off steam cloud synchronization. What to do if wallpaperengine downloads other people's wallpapers after logging into another account 1. Log in to your own steam account, find cloud synchronization in settings, and turn off steam cloud synchronization. 2. Log in to someone else's Steam account you logged in before, open the Wallpaper Creative Workshop, find the subscription content, and then cancel all subscriptions. (In case you cannot find the wallpaper in the future, you can collect it first and then cancel the subscription) 3. Switch back to your own steam

With the rapid development of social media, Xiaohongshu has become a popular platform for many young people to share their lives and explore new products. During use, sometimes users may encounter difficulties logging into previous accounts. This article will discuss in detail how to solve the problem of logging into the old account on Xiaohongshu, and how to deal with the possibility of losing the original account after changing the binding. 1. How to log in to Xiaohongshu’s previous account? 1. Retrieve password and log in. If you do not log in to Xiaohongshu for a long time, your account may be recycled by the system. In order to restore access rights, you can try to log in to your account again by retrieving your password. The operation steps are as follows: (1) Open the Xiaohongshu App or official website and click the "Login" button. (2) Select "Retrieve Password". (3) Enter the mobile phone number you used when registering your account

The solution to the Discuz background login problem is revealed. Specific code examples are needed. With the rapid development of the Internet, website construction has become more and more common, and Discuz, as a commonly used forum website building system, has been favored by many webmasters. However, precisely because of its powerful functions, sometimes we encounter some problems when using Discuz, such as background login problems. Today, we will reveal the solution to the Discuz background login problem and provide specific code examples. We hope to help those in need.

Recently, some friends have asked me how to log in to the Kuaishou computer version. Here is the login method for the Kuaishou computer version. Friends who need it can come and learn more. Step 1: First, search Kuaishou official website on Baidu on your computer’s browser. Step 2: Select the first item in the search results list. Step 3: After entering the main page of Kuaishou official website, click on the video option. Step 4: Click on the user avatar in the upper right corner. Step 5: Click the QR code to log in in the pop-up login menu. Step 6: Then open Kuaishou on your phone and click on the icon in the upper left corner. Step 7: Click on the QR code logo. Step 8: After clicking the scan icon in the upper right corner of the My QR code interface, scan the QR code on your computer. Step 9: Finally log in to the computer version of Kuaishou

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Baidu Netdisk can not only store various software resources, but also share them with others. It supports multi-terminal synchronization. If your computer does not have a client downloaded, you can choose to enter the web version. So how to log in to Baidu Netdisk web version? Let’s take a look at the detailed introduction. Baidu Netdisk web version login entrance: https://pan.baidu.com (copy the link to open in the browser) Software introduction 1. Sharing Provides file sharing function, users can organize files and share them with friends in need. 2. Cloud: It does not take up too much memory. Most files are saved in the cloud, effectively saving computer space. 3. Photo album: Supports the cloud photo album function, import photos to the cloud disk, and then organize them for everyone to view.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

What should I do if I can’t log in to my account on Google Chrome? When many users use this software, certain functions require users to log in to their Google account before they can use it. However, they have tried many times but failed to log in successfully. Faced with this problem, many users do not know how to solve it, so In this issue, the editor is here to share the solution with you. I hope that the content of today’s software tutorial can be helpful to everyone. The solution is as follows: 1. Click on a browser on the desktop, and after opening it, you will see something like this. 2. If a login pops up at this time, click it. If you can't see it, click the upper right corner. 3. Click Login, then enter your account number. You do not need to enter the account after @, and click Next. 4. Enter the password. When you see this prompt, click Enable
