search
Homephp教程php手册Yii使用Captcha验证码的方法,yiicaptcha验证码

Yii使用Captcha验证码的方法,yiicaptcha验证码

本文实例讲述了Yii使用Captcha验证码的方法。分享给大家供大家参考,具体如下:

详细代码可参考:yii自带的示例代码post项目,里面有一个contact表单用到了验证码.

1. Model:

将验证码加入UserLogin的一个属性:

class UserLogin extends CFormModel
{
 public $username;
 public $password;
 public $rememberMe;
 public $verifyCode;
 public function rules()
 {
  return array(
   // username and password are required
   array('username, password,verifyCode', 'required'),
   // rememberMe needs to be a boolean
   array('rememberMe', 'boolean'),
   // password needs to be authenticated
   array('password', 'authenticate'),
   // verifyCode needs to be entered correctly
   array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
  );
 }
 /**
  * Declares attribute labels.
  */
 public function attributeLabels()
 {
  return array(
   'rememberMe'=>Yii::t('user',"Remember me next time"),
   'username'=>Yii::t('user',"username or email"),
   'password'=>Yii::t('user',"password"),
   'verifyCode'=>Yii::t('user','Verification Code'),
  );
 }
}

2. Controller

在LoginController控制器加入映射动作CCaptchaAction

public function actions()
{
 return array(
  // captcha action renders the CAPTCHA image displayed on the contact page
  'captcha'=>array(
   'class'=>'CCaptchaAction',
   'backColor'=>0xf4f4f4,
   'padding'=>0,
   'height'=>30,
   'maxLength'=>4,
  ),
  );
}
ublic function actionLogin()
{
 if (Yii::app()->user->isGuest) {
  $model=new UserLogin;
  // collect user input data
  if(isset($_POST['UserLogin']))
  {
   $model->attributes=$_POST['UserLogin'];
//在此核对验证码
   if($this->createAction('captcha')->validate($model->verifyCode, false))
   {
    // validate user input and redirect to previous page if valid
    if($model->validate()) {
    //admin login only
    if( Yii::app()->getModule('user')->isAdmin()==1 )
    {
    $this->lastViset();
    if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
     $this->redirect(Yii::app()->controller->module->returnUrl);
    else
     $this->redirect(Yii::app()->user->returnUrl);
    }else
    {//if no admin when login out
     $this->redirect(Yii::app()->controller->module->logoutUrl);
    }
   }
   }else
   {//提示错误
    $model->addError('verifyCode','验证码不对');
   }
  }
  // display the login form
  $this->render('/user/login',array('model'=>$model));
 } else
  $this->redirect(Yii::app()->controller->module->returnUrl);
}

在验证用户名密码前,检查验证码:

if($this->createAction('captcha')->validate($model->verifyCode, false))
{

3. view

在视图中显示验证码图片,输入框

<&#63;php $this->widget('CCaptcha'); &#63;>
  <&#63;php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); &#63;>
<img src="/static/imghwm/default1.png"  data-src="http://www.XXXX.net/uploads/123456.jpg"  class="lazy" alt="">

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • yii用户注册表单验证实例
  • PHP Yii框架之表单验证规则大全
  • Yii框架form表单用法实例
  • Yii不依赖Model的表单生成器用法实例
  • yii框架表单模型使用及以数组形式提交表单数据示例
  • Yii中实现处理前后台登录的新方法
  • yii去掉必填项中星号的方法
Statement
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
如何简单绕过人机身份验证Captcha如何简单绕过人机身份验证CaptchaMay 11, 2023 pm 05:55 PM

今天分享的Writeup是作者在目标网站漏洞测试中发现的一种简单的人机身份验证(Captcha)绕过方法,利用Chrome开发者工具对目标网站登录页面进行了简单的元素编辑就实现了Captcha绕过。人机身份验证(Captcha)通常会出现在网站的注册、登录和密码重置页面,以下是目标网站在登录页面中布置的Captcha机制。从上图中可以看到,用户只有在勾选了Captcha验证机制的“I‘mnotarobot”之后,登录按钮(Sign-IN)才会启用显示以供用户点击。因此,基于这点,我右键点击了Si

利用Beego和Captcha实现验证码功能利用Beego和Captcha实现验证码功能Jun 22, 2023 pm 10:54 PM

随着互联网的发展和普及,越来越多的网站和应用程序被开发出来,其中很多应用程序都需要输入验证码以确保用户的有效性和安全性。本文将介绍如何使用Beego框架和Captcha库来实现验证码功能。其中Beego是一款基于Go语言的Web应用框架,Captcha是一个Go语言开源的验证码库。Beego框架的准备工作在实现验证码之前,需要先安装Beego框架并创建一个新

php如何使用Yii3框架?php如何使用Yii3框架?May 31, 2023 pm 10:42 PM

随着互联网的不断发展,Web应用程序开发的需求也越来越高。对于开发人员而言,开发应用程序需要一个稳定、高效、强大的框架,这样可以提高开发效率。Yii是一款领先的高性能PHP框架,它提供了丰富的特性和良好的性能。Yii3是Yii框架的下一代版本,它在Yii2的基础上进一步优化了性能和代码质量。在这篇文章中,我们将介绍如何使用Yii3框架来开发PHP应用程序。

如何使用PHP框架Yii开发一个高可用的云备份系统如何使用PHP框架Yii开发一个高可用的云备份系统Jun 27, 2023 am 09:04 AM

随着云计算技术的不断发展,数据的备份已经成为了每个企业必须要做的事情。在这样的背景下,开发一款高可用的云备份系统尤为重要。而PHP框架Yii是一款功能强大的框架,可以帮助开发者快速构建高性能的Web应用程序。下面将介绍如何使用Yii框架开发一款高可用的云备份系统。设计数据库模型在Yii框架中,数据库模型是非常重要的一部分。因为数据备份系统需要用到很多的表和关

Yii2 vs Phalcon:哪个框架更适合开发显卡渲染应用?Yii2 vs Phalcon:哪个框架更适合开发显卡渲染应用?Jun 19, 2023 am 08:09 AM

在当前信息时代,大数据、人工智能、云计算等技术已经成为了各大企业关注的热点。在这些技术中,显卡渲染技术作为一种高性能图形处理技术,受到了越来越多的关注。显卡渲染技术被广泛应用于游戏开发、影视特效、工程建模等领域。而对于开发者来说,选择一个适合自己项目的框架,是一个非常重要的决策。在当前的语言中,PHP是一种颇具活力的语言,一些优秀的PHP框架如Yii2、Ph

Symfony vs Yii2:哪个框架更适合开发大型Web应用?Symfony vs Yii2:哪个框架更适合开发大型Web应用?Jun 19, 2023 am 10:57 AM

随着Web应用需求的不断增长,开发者们在选择开发框架方面也越来越有选择的余地。Symfony和Yii2是两个备受欢迎的PHP框架,它们都具有强大的功能和性能,但在面对需要开发大型Web应用时,哪个框架更适合呢?接下来我们将对Symphony和Yii2进行比较分析,以帮助你更好地进行选择。基本概述Symphony是一个由PHP编写的开源Web应用框架,它是建立

Yii框架中的数据查询:高效地访问数据Yii框架中的数据查询:高效地访问数据Jun 21, 2023 am 11:22 AM

Yii框架是一个开源的PHPWeb应用程序框架,提供了众多的工具和组件,简化了Web应用程序开发的流程,其中数据查询是其中一个重要的组件之一。在Yii框架中,我们可以使用类似SQL的语法来访问数据库,从而高效地查询和操作数据。Yii框架的查询构建器主要包括以下几种类型:ActiveRecord查询、QueryBuilder查询、命令查询和原始SQL查询

yii如何将对象转化为数组或直接输出为json格式yii如何将对象转化为数组或直接输出为json格式Jan 08, 2021 am 10:13 AM

yii框架:本文为大家介绍了yii将对象转化为数组或直接输出为json格式的方法,具有一定的参考价值,希望能够帮助到大家。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment