yii中的登录如何实现

yii中的登录如何实现

yii中的登录如何实现

1、创建数据表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;

yii中的登录如何实现

2、登陆页面

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

3、控制器

相关文章教程推荐:yii教程

<?php namespace appcontrollers; use yiiwebController; use appmodelsAdmin; use Yii;   class IndexController extends Controller {     public function actionIndex()     { //      不使用布局         $this->layout = false;         $model = new Admin; //        是否是post提交         if (Yii::$app-&gt;request-&gt;isPost) { //            获得post提交参数             $post = Yii::$app-&gt;request-&gt;post();             if($model-&gt;login($post)){                 return "登陆成功";             } else {                 return "登陆失败";             }         } else {             return $this-&gt;render("index", ['model' =&gt; $model]);         }     }   }

4、模型

<?php namespace appmodels; use yiidbActiveRecord; 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()                 -&gt;where(['adminuser' =&gt; $this-&gt;adminuser])                 -&gt;andwhere(['adminpass' =&gt; md5($this-&gt;adminpass)])                 -&gt;one();             if (is_null($data)) {                 $this-&gt;addError('adminpass', 'adminuser or adminpass error');             }         }     }     public function login($data)     {         if($this-&gt;load($data) &amp;&amp; $this-&gt;validate()) { //            登陆信息写入session             $session = Yii::$app-&gt;session;             $session-&gt;open();             $session-&gt;set('adminuser', $this-&gt;adminuser); //           更新登陆时间和IP             $this-&gt;updateAll(['logintime' =&gt; time(), 'loginip' =&gt; ip2long(Yii::$app-&gt;request-&gt;userIP)], ['adminuser' =&gt; $this-&gt;adminuser]);             return true;         }         return false;     } }

更多yiiyii教程教程,请关注PHP中文网。        

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享