Home > PHP Framework > YII > How to implement login in yii

How to implement login in yii

angryTom
Release: 2020-02-18 11:01:24
Original
1896 people have browsed it

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;
Copy after login

How to implement login in yii

2. Login page

<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
$form = ActiveForm::begin([
    &#39;id&#39; => &#39;abc-form&#39;,
    &#39;options&#39; => [&#39;class&#39; => &#39;form-horizontal&#39;],
])?>
<?= $form->field($model, &#39;adminuser&#39;)->textInput([&#39;placeholder&#39; => "用户名"])->label(&#39;账号&#39;) ?>
<?= $form->field($model, &#39;adminpass&#39;)->passwordInput()->label(&#39;密码&#39;) ?>
<?= Html::submitButton(&#39;提交&#39;) ?>
<?php ActiveForm::end() ?>
Copy after login

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", [&#39;model&#39; => $model]);
        }
    }
 
}
Copy after login

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 [
            [&#39;adminuser&#39;, &#39;required&#39;],
            [&#39;adminpass&#39;, &#39;required&#39;],
//           验证密码是否正确
            [&#39;adminpass&#39;, &#39;validatePass&#39;]
        ];
    }
 
    public function validatePass()
    {
        if (!$this->hasErrors()) {
//            判断用户名密码是否正确
            $data = self::find()
                ->where([&#39;adminuser&#39; => $this->adminuser])
                ->andwhere([&#39;adminpass&#39; => md5($this->adminpass)])
                ->one();
            if (is_null($data)) {
                $this->addError(&#39;adminpass&#39;, &#39;adminuser or adminpass error&#39;);
            }
        }
    }
    public function login($data)
    {
        if($this->load($data) && $this->validate()) {
//            登陆信息写入session
            $session = Yii::$app->session;
            $session->open();
            $session->set(&#39;adminuser&#39;, $this->adminuser);
//           更新登陆时间和IP
            $this->updateAll([&#39;logintime&#39; => time(), &#39;loginip&#39; => ip2long(Yii::$app->request->userIP)], [&#39;adminuser&#39; => $this->adminuser]);
            return true;
        }
        return false;
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template