yii添加验证码 和重复密码

<?phpnamespace frontend\models;

use common\models\User;use yii\base\Model;use Yii;

/** * Signup form */class SignupForm extends Model{    public $username;    public $email;    public $password;    public $rePassword;    public $vitifyCode;

/**     * @inheritdoc     */    public function rules()    {        return [            [‘username‘, ‘filter‘, ‘filter‘ => ‘trim‘],            [‘username‘, ‘required‘],            [‘username‘, ‘unique‘, ‘targetClass‘ => ‘\common\models\User‘, ‘message‘ => ‘This username has already been taken.‘],            [‘username‘, ‘string‘, ‘min‘ => 2, ‘max‘ => 255],

[‘email‘, ‘filter‘, ‘filter‘ => ‘trim‘],            [‘email‘, ‘required‘],            [‘email‘, ‘email‘],//格式必须是邮箱            [‘email‘, ‘string‘, ‘max‘ => 255],            [‘email‘, ‘unique‘, ‘targetClass‘ => ‘\common\models\User‘, ‘message‘ => ‘This email address has already been taken.‘],

[[‘password‘, ‘rePassword‘], ‘required‘],            [[‘password‘, ‘rePassword‘], ‘string‘, ‘min‘ => 6],            [‘rePassword‘, ‘compare‘, ‘compareAttribute‘ => ‘password‘, ‘message‘ => ‘两次密码必须一致‘], //两次密码必须一致            [‘vitifyCode‘, ‘captcha‘], //验证码验证        ];    }

public function attributeLabels() //属性labels    {        return [            ‘username‘ => ‘用户名‘,            ‘email‘ => ‘邮箱‘,            ‘password‘ => ‘密码‘,            ‘rePassword‘ => ‘重复密码‘,            ‘vitifyCode‘ => ‘验证码‘,        ];    }

/**     * Signs user up.     *     * @return User|null the saved model or null if saving fails     */    public function signup()    {        if ($this->validate()) {            $user = new User();            $user->username = $this->username;            $user->email = $this->email;            $user->setPassword($this->password);            $user->generateAuthKey();            if ($user->save()) {                return $user;            }        }

return null;    }}?>
<?phpnamespace frontend\controllers;

use Yii;use common\models\LoginForm;use frontend\models\PasswordResetRequestForm;use frontend\models\ResetPasswordForm;use frontend\models\SignupForm;use frontend\models\ContactForm;use yii\base\InvalidParamException;use yii\web\BadRequestHttpException;use yii\web\Controller;use yii\filters\VerbFilter;use yii\filters\AccessControl;
/** * Site controller */class SiteController extends Controller{    /**     * @inheritdoc     */    public function behaviors()    {        return [            ‘access‘ => [                ‘class‘ => AccessControl::className(),                ‘only‘ => [‘logout‘, ‘signup‘],                ‘rules‘ => [                    [                        ‘actions‘ => [‘signup‘],                        ‘allow‘ => true,                        ‘roles‘ => [‘?‘],                    ],                    [                        ‘actions‘ => [‘logout‘],                        ‘allow‘ => true,                        ‘roles‘ => [‘@‘],                    ],                ],            ],            ‘verbs‘ => [                ‘class‘ => VerbFilter::className(),                ‘actions‘ => [                    ‘logout‘ => [‘post‘, ‘get‘],                ],            ],        ];    }

/**     * @inheritdoc     */    public function actions()    {        return [            ‘error‘ => [                ‘class‘ => ‘yii\web\ErrorAction‘,            ],            ‘captcha‘ => [                ‘class‘ => ‘yii\captcha\CaptchaAction‘,                ‘fixedVerifyCode‘ => YII_ENV_TEST ? ‘testme‘ : null,            ],            ‘upload‘ => [                ‘class‘ => ‘frontend\widgets\ueditor\UEditorAction‘            ],

];    }
/** * Signs user up. * * @return mixed */public function actionSignup(){    $model = new SignupForm();    if ($model->load(Yii::$app->request->post())) {        if ($user = $model->signup()) {            if (Yii::$app->getUser()->login($user)) {                return $this->goHome();            }        }    }

return $this->render(‘signup‘, [        ‘model‘ => $model,    ]);}
?>
<?php

/* @var $this yii\web\View *//* @var $form yii\bootstrap\ActiveForm *//* @var $model \frontend\models\SignupForm */

use yii\helpers\Html;use yii\bootstrap\ActiveForm;

$this->title = ‘Signup‘;$this->params[‘breadcrumbs‘][] = $this->title;?><div class="site-signup">    <h1><?= Html::encode($this->title) ?></h1>

<p>Please fill out the following fields to signup:</p>

<div class="row">        <div class="col-lg-5">            <?php $form = ActiveForm::begin([‘id‘ => ‘form-signup‘]); ?>

<?= $form->field($model, ‘username‘) ?>

<?= $form->field($model, ‘email‘) ?>

<?= $form->field($model, ‘password‘)->passwordInput() ?>

<?= $form->field($model, ‘rePassword‘)->passwordInput() ?>

<?= $form->field($model, ‘vitifyCode‘)->widget(\yii\captcha\Captcha::className()) ?> //验证码组件调用

<div class="form-group">                    <?= Html::submitButton(‘Signup‘, [‘class‘ => ‘btn btn-primary‘, ‘name‘ => ‘signup-button‘]) ?>                </div>

<?php ActiveForm::end(); ?>        </div>    </div></div>?>
时间: 2024-10-14 23:46:53

yii添加验证码 和重复密码的相关文章

Yii添加验证码

添加带验证码的登陆: 1.先在模型modules下的LoginForm.php定义一个存储验证码的变量:public $verfyCode: 2.然后在rules()方法里定义:array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()): 3.然后在对应视图views下的login.php里定义: <?php if(CCaptcha::checkRequirements()): ?> <d

yii登陆中添加验证码

1.在SiteController中添加如下代码: Php代码   /** * Declares class-based actions. */ public function actions() { return array( // captcha action renders the CAPTCHA image displayed on the contact page 'captcha' => array( 'class' => 'CCaptchaAction', 'backColor'

yii2中添加验证码的实现方法

本文实例讲述了yii2中添加验证码的实现方法.分享给大家供大家参考,具体如下: 首先,在模型中添加验证码字段: ? 1 2 3 public function rules(){ return ['verifyCode', 'captcha'], } 其次,可以在函数attributeLabels中添加前台页面中验证码的字段名称: ? 1 2 3 public function atrributeLabels(){ return ['verifyCode'=>'Verification Code'

【试水CAS-4.0.3】第03节_CAS服务端登录页添加验证码

/** * @see ------------------------------------------------------------------------------------------------------------------------ * @see CAS登录页添加验证码 * @see 0.这年头验证码一般用来防止帐号被暴力破解,如果我们的系统是走专线的,也就是说放在内网,那完全没必要搞验证码 * @see 1.由于CAS使用了Spring Web Flow框架,所以

前后端分离之后添加验证码

转载自:http://www.cnblogs.com/liminjun88/p/6556493.html#commentform 1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服务的方式,所以后台不止3个,画图示意.终极方案是采用Docker,在前端和后台调用中间添加一层:API Gateway. 因为考虑到和其他系统集成的可能性,所以在登录这一

Membership添加验证码登录

1.在公共类ImageHelper中编写公共方法,产生随机验证码 /// <summary> /// 产生随机验证码 /// </summary> /// <returns></returns> public string GetString() { string randString = ""; Random random = new Random(); do { //使用DateTime.Now.Millisecond作为生成随机数的

WordPress优化:给后台登录添加验证码以及登录限制

为了防止后台被某些黑客进行密码爆破,添加验证码以及登录限制是很有必要的.这里我分享两个插件,分别是:SI CAPTCHA Anti-Spam 和 Limit Login Attempts .一个是验证码,另一个是限制登录尝试.这两个插件直接在网站后台搜索安装就可以了 开启两个插件后,后台登录效果如下: 好了,有兴趣的小伙伴可以试试O(∩_∩)O~

PHPCMS v9 自定义表单添加验证码验证

1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id="code" name="code" size="8" class="input-text">{form::checkcode('code_img', '4', '14', 84, 24)} 2. 在 \phpcms\modules\

PHPCMS v9 自定义表单添加验证码

1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id="code" name="code" size="8" class="input-text">{form::checkcode('code_img', '4', '14', 84, 24)} 2. 在 \phpcms\modules