我遇到一个关于CSRF的问题,总是出现Error:The CSRF token could not be verified.
[color="#FF0000"]我的VIEW代码是:[/color]
<div id="formcenter">
<h2><?php echo Yii::t('register', 'Registration Form'); ?></h2>
<p><?php echo Yii::t('register', 'Please fill all required fields and hit the submit button once your done.'); ?></p>
<?php if($model->hasErrors()): ?>
<div class="errordiv">
<?php echo CHtml::errorSummary($model); ?>
</div>
<?php endif; ?>
<?php echo CHtml::form('', 'post', array('class'=>'frmcontact')); ?>
<div>
<?php echo CHtml::activeLabel($model, 'username'); ?>
<?php echo CHtml::activeTextField($model, 'username', array( 'class' => 'textboxcontact tiptopfocus', 'title' => Yii::t('register', 'Enter your desired username (Min: 3 Max: 32)') )); ?>
<?php echo CHtml::error($model, 'username', array( 'class' => 'errorfield' )); ?>
<br />
<?php echo CHtml::activeLabel($model, 'password'); ?>
<?php echo CHtml::activePasswordField($model, 'password', array( 'class' => 'textboxcontact tiptopfocus', 'title' => Yii::t('register', 'Enter your desired password (Min: 3 Max: 32)') )); ?>
<?php echo CHtml::error($model, 'password', array( 'class' => 'errorfield' )); ?>
<br />
<?php echo CHtml::activeLabel($model, 'password2'); ?>
<?php echo CHtml::activePasswordField($model, 'password2', array( 'class' => 'textboxcontact tiptopfocus', 'title' => Yii::t('register', 'Confirm your password') )); ?>
<?php echo CHtml::error($model, 'password2', array( 'class' => 'errorfield' )); ?>
<br />
<?php echo CHtml::activeLabel($model, 'email'); ?>
<?php echo CHtml::activeTextField($model, 'email', array( 'class' => 'textboxcontact tiptopfocus', 'title' => Yii::t('register', 'Enter your desired email address') )); ?>
<?php echo CHtml::error($model, 'email', array( 'class' => 'errorfield' )); ?>
<br />
<?php echo CHtml::activeLabel($model, 'verifyCode'); ?>
<?php echo CHtml::activeTextField($model, 'verifyCode', array( 'class' => 'textboxcontact tiptopfocus', 'title' => Yii::t('register', 'Enter the text displayed in the image below') )); ?>
<?php echo CHtml::error($model, 'verifyCode', array( 'class' => 'errorfield' )); ?>
<br />
<?php $this->widget('CCaptcha'); ?>
<br /><br /><br />
<p>
<?php echo CHtml::submitButton(Yii::t('global', 'Submit'), array('class'=>'submitcomment', 'name'=>'submit')); ?>
</p>
</div>
<?php echo CHtml::endForm(); ?>
</div>
[color="#FF0000"]我的CONTROL代码是:[/color]
<?php
/**
- Register controller class
*/
class RegisterController extends SiteBaseController
{
/**
* Controller constructor
*/
public function init()
{
// Do not allow logged in users here
if( Yii::app()->user->id )
{
$this->redirect('index/index');
}
// Add page breadcrumb and title
$this->pageTitle[] = Yii::t('global', 'Register');
$this->breadcrumbs[ Yii::t('global', 'Register') ] = array('register/index');
parent::init();
}
/**
* List of available actions
*/
public function actions()
{
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'backColor' => 0xFFFFFF,
'minLength' => 3,
'maxLength' => 4,
'testLimit' => 3,
'padding' => array_rand( range( 2, 10 ) ),
),
);
}
/**
* Register action
*/
public function actionindex()
{
$model = new RegisterForm;
if( isset($_POST['RegisterForm']) )
{
$model->attributes = $_POST['RegisterForm'];
if( $model->validate() )
{
// Save the member and redirect
$user = new Members;
$user->scenario = 'register';
$user->role = 'member';
$user->attributes = $_POST['RegisterForm'];
$user->save();
// Redirect
Yii::app()->user->setFlash('success', Yii::t('register', 'Registration Completed. Please sign in.'));
$this->redirect('login/index');
}
}
$this->render('index', array('model'=>$model));
}
}
[color="#FF0000"]我的model是这样的:[/color]
<?php
/**
- Register form model
*/
class RegisterForm extends CFormModel
{
/**
* @var string - username
*/
public $username;
/**
* @var string - password
*/
public $password;
/**
* @var string - password2
*/
public $password2;
/**
* @var string - email
*/
public $email;
/**
* @var string - captcha
*/
public $verifyCode;
/**
* table data rules
*
* @return array
*/
public function rules()
{
return array(
array('username', 'match', 'allowEmpty' => false, 'pattern' => '/[A-Za-z0-9\x80-\xFF]+$/' ),
array('email', 'email'),
array('email, username', 'unique', 'className' => 'Members' ),
array('email, password, password2', 'required'),
array('username, password, password2', 'length', 'min' => 3, 'max' => 32),
array('password2', 'compare', 'compareAttribute'=>'password'),
array('email', 'length', 'min' => 3, 'max' => 55),
array('verifyCode', 'captcha'),
);
}
/**
* Attribute values
*
* @return array
*/
public function attributeLabels()
{
return array(
'username' => Yii::t('members', 'Username'),
'email' => Yii::t('members', 'Email'),
'password' => Yii::t('members', 'Password'),
'password2' => Yii::t('members', 'Password Confirmation'),
'verifyCode' => Yii::t('members', 'Security Code'),
);
}
}
按照YII的说明:
要显示一个表单,请使用CHtml::form而不要自己写HTML代码。因为CHtml::form可以自动地在表单中嵌入一个隐藏项,这个隐藏项储存着验证所需的随机数据,这些数据可在表单提交的时候发送到服务器进行验证。
那么:
(1)我已经将本机中的IE7的cookies开启了,可以排除cookie的问题。
(2)我上面的代码使用CHtml::form,应该是可以的啊?
为什么不行呢?应该如何修改呢?
谢谢各位的帮助啊!