Heim > PHP-Framework > YII > Hauptteil

So implementieren Sie die Anmeldung in yii

angryTom
Freigeben: 2020-02-18 11:01:24
Original
1851 Leute haben es durchsucht

So implementieren Sie die Anmeldung in yii

So implementieren Sie die Anmeldung in yii

1. Erstellen Sie die Datentabelle 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;
Nach dem Login kopieren

So implementieren Sie die Anmeldung in yii

2. Anmeldeseite

<?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() ?>
Nach dem Login kopieren

3. Controller

Empfohlene verwandte Artikel und Tutorials: 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]);
        }
    }
 
}
Nach dem Login kopieren

4. Modell

<?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;
    }
}
Nach dem Login kopieren

Weitere yiiEinführung in die Programmierung-Tutorials finden Sie auf der chinesischen PHP-Website .       

Das obige ist der detaillierte Inhalt vonSo implementieren Sie die Anmeldung in yii. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!