Adding CAPTCHA
Nowadays, on the Internet, if you leave a form without a spam protection, you will get a ton of spam data entered in a short time. Yii includes a CAPTCHA component that makes adding such a protection a breeze. The only problem is that there is no systematic guide on how to use it.
In the following example, we will add a CAPTCHA protection to a simple form.
Getting ready
Create a fresh application using
yiic webapp.Create a form model named
protected/models/EmailForm.phpas follows:<?php class EmailForm extends CFormModel { public $email; function rules(){ return array( array('email', 'email'), ); } }Create a controller named
protected/controllers/EmailController.phpas follows:<?php class EmailController extends Controller { public function actionIndex() { $success = false; $model = new EmailForm(); if(!empty($_POST['EmailForm'])) { $model->setAttributes($_POST['EmailForm']); if($model...