关于验证码实现的简单做法

各个框架都有自己的验证码写法,yii框架也不例外!童鞋们一起来学php

1,给大家介绍一下控制器的写法

<?php
namespace app\controllers;
use Yii;
use app\models\User8;
use yii\web\Controller;
use yii\web\NotFoundHttpException;


class LoginController extends Controller
{
  
   
       public function actionVerify()  

        {  
          $request = Yii::$app->request;
         if ($request->isPost)
         {
            $request = Yii::$app->request->post();
            $name = $request['user_name'];
            $password = $request['user_password'];
         
            $info = Yii::$app->db->createCommand("SELECT * FROM  表名where 字段 = '"变量."' and 字段= '".变量."'")->queryOne();

            也有时会用session存值来实现用户名查找

             $session = Yii::$app->session;
            $session->open();
            $session->set("user_id",$info['user_id']);
               return $this->redirect(['login/list']);
         }else{

              验证码部分 这里我们自己写的模型
            $model = new User8();  

            return  $this->render('verify',['model'=>$model]);  

         }


 }

------------------------------------------------------------------------

2,在model我们也要做一些改动

告诉大家一定要在models里创建一个文件夹名字叫User8.php

名字大家看的叫这里我告大家

<?php  
 
namespace app\models;  
 
use Yii;  
 
use yii\captcha\Captcha;  
 
class User8 extends \yii\db\ActiveRecord  
 
{  
 
    public $verifyCode;  
 
    public static function tableName()  
 
    {  
 
        return 'user8';  
 
    }  
 
    public function rules()  
 
    {  
 
        return [  
 
            [['username', 'password'], 'string', 'max' => 255],  
 
            ['verifyCode', 'captcha','message'=>'验证码错误']  
 
        ];  
 
    }  
 

3然后v层我们也可已有自己的一些写法我们都不能错过呀

?>

<?php $form = ActiveForm::begin(['action'=>Url::to(['login/verify']),
'method'=>'post',

'options' => ['enctype' => 'multipart/form-data']]) ?>

垃圾代码太多大家要小心

    <h1>用户登录页面</h1>
<table>
<tr>
    <td>用户名</td>
    <td><input type="text" name="user_name"></td>
</tr>
<tr>
    <td>密码</td>
    <td><input type="password" name="user_password"></td>
</tr>
    <tr>
    <td>验证码</td>
    <td>


<?= $form->field($model, 'verifyCode')->widget(Captcha::className(),
[ 'template' =>'<div class="row">
<div class="col-lg-3">{image}</div>
<div class="col-lg-6">{input}
</div>
</div>',  
]) ?>  
 
 


    </td>
    </tr>
<tr>
    <td><input type="submit" value="登录"></td>
    <td></td>
</tr>
</table>
<?php ActiveForm::end() ?>

-4.如果我们还想进一步改变他的样式我们可以在我的 c 层找到一个交site的控制器里不他的 -----function actions()   方法替换成我们的样式-------------------------------

public function actions()  
 
{  
 
   return [  
 
   'error' => [  
 
   'class' => 'yii\web\ErrorAction',  
 
   ],  
 
   'captcha' => [  
 
   'class' => 'yii\captcha\CaptchaAction',  
 
   'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,//调整验证码是数字还是中文  
 
   // 'fixedVerifyCode' => substr(time(),mt_rand(1,9),5),  
 
   'backColor'=>0x000000,//背景颜色  
 
   'maxLength' => 3, //最大显示个数  
 
   'minLength' => 3,//最少显示个数  
 
   'padding' => 4,//间距  
 
   'height'=>40,//高度  
 
   'width' => 70,  //宽度    
 
   'foreColor'=>0xffffff,     //字体颜色  
 
   'offset'=>6,        //设置字符偏移量 有效果  
 
   ],  
 
 ];  
 
}
总之一个不是问题的问解决了,告诉大家我只是一些自己的写法,我们一起进步



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值