Captcha的session问题,疑似bug

我是通过ajax来提交验证码,可是总是返回false, 为了查找原因,我分别在官方blog demo和我自己的一个项目里部署了一模一样的代码,可是在blog demo里却完全正常工作,在我的项目里却总是返回false,困惑了我两天,先看看我自己项目的代码,代码本身应该没有问题:

先是model:

<?php

| class DCaptcha extends CFormModel

| {

| public $verifyCode;

|

|

| /**

| * @return array validation rules for model attributes.

| */

| public function rules()

| {

| return array(

| array(‘verifyCode’, ‘captcha’,‘allowEmpty’=> !extension_loaded(‘gd’))

| );

| }

|

| /**

| * @return array customized attribute labels (name=>label)

| */

| public function attributeLabels()

| {

| return array(

| ‘verifyCode’=>‘Verification Code’,

| );

| }

|

| }

| ?>

然后是controller:

<?php

|

| class DaogouController extends CController

| {

|

| public function actions() {

| return array(

| ‘captcha’=> array(

| ‘class’ => ‘CCaptchaAction’,

| ‘backColor’ => 0xffffff,

| ‘testLimit’=>999,

| ),

| );

| }

  public  function actionCheckCaptcha()

| {

| $_SESSION = array();

| var_dump($_SESSION);

| $data = $_GET[‘ccode’];

| $result = array(‘state’=>false,‘msg’=>’’);

| $captcha = new DCaptcha();

| $captcha->verifyCode = $data;

| $r = $captcha->validate();

| var_dump($_SESSION);

| var_dump($r);

| var_dump($captcha);

| return $r;

| }

| }

最后是videw:

<input type="text" class="input_field name" id="register_code"/>

<div class=“captcha” ><?php $this->Widget(“CCaptcha”,array(‘buttonLabel’=>‘换一张’)); ?></div>

和ajax请求:

$(’#register_code’).blur(function(){

     var obj = &#036;(this);


     var val = obj.attr(&quot;value&quot;);

$.getJSON("/index.php/[color="#FF0000"]daogou[/color]/checkCaptcha?ccode="+val+"&random="+Math.random(),function(data){});

});

再看看在blog demo里的ajax请求:

$.getJSON("/index.php?r=[color="#FF0000"]site[/color]/checkcaptcha&ccode="+val+"&random="+Math.random(),function(data){});

注意ajax请求中controller的区别,下面是对我的项目操作的输出结果:

正确输入code:

964

2010-11-21_182636.jpg

打印的结果:

array(0) {

}

array(4) { [color="#8B0000"]//正确的code被保存到了site.captcha下,而保存在daogou.captcha下的code不知道从哪里来的,所以,不管这么样总是返回false,why?[/color]

["Yii.CCaptchaAction.dfd593a.[color="#FF0000"]site[/color].captcha"]=>

string(7) "zovacar"

["Yii.CCaptchaAction.dfd593a.site.captchacount"]=>

int(1)

["Yii.CCaptchaAction.dfd593a.[color="#FF0000"]daogou[/color].captcha"]=>

string(7) "zobaywt"

["Yii.CCaptchaAction.dfd593a.daogou.captchacount"]=>

int(26)

}

bool(false)

object(DCaptcha)#13 (6) {

["verifyCode"]=>

string(7) "zovacar"

["_errors":"CModel":private]=>

array(1) {

[&quot;verifyCode&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(35) &quot;The verification code is incorrect.&quot;


}

}

["_validators":"CModel":private]=>

object(CList)#14 (5) {

[&quot;_d&quot;:&quot;CList&quot;:private]=&gt;


array(1) {


  [0]=&gt;


  object(CCaptchaValidator)#15 (10) {


    [&quot;caseSensitive&quot;]=&gt;


    bool(false)


    [&quot;captchaAction&quot;]=&gt;


    string(7) &quot;captcha&quot;


    [&quot;allowEmpty&quot;]=&gt;


    bool(false)


    [&quot;attributes&quot;]=&gt;


    array(1) {


      [0]=&gt;


      string(10) &quot;verifyCode&quot;


    }


    [&quot;message&quot;]=&gt;


    NULL


    [&quot;skipOnError&quot;]=&gt;


    bool(false)


    [&quot;on&quot;]=&gt;


    array(0) {


    }


    [&quot;safe&quot;]=&gt;


    bool(true)


    [&quot;_e&quot;:&quot;CComponent&quot;:private]=&gt;


    NULL


    [&quot;_m&quot;:&quot;CComponent&quot;:private]=&gt;


    NULL


  }


}


[&quot;_c&quot;:&quot;CList&quot;:private]=&gt;


int(1)


[&quot;_r&quot;:&quot;CList&quot;:private]=&gt;


bool(false)


[&quot;_e&quot;:&quot;CComponent&quot;:private]=&gt;


NULL


[&quot;_m&quot;:&quot;CComponent&quot;:private]=&gt;


NULL

}

["_scenario":"CModel":private]=>

string(0) ""

["_e":"CComponent":private]=>

NULL

["_m":"CComponent":private]=>

NULL

}

谁能告诉我是什么原因?我该怎么做才行呢? 我用的是v1.1.4

把 rules() 改成试试:

public function rules()

{

return array(

array('verifyCode', 'captcha', [color=&quot;#FF0000&quot;]'captchaAction' =&gt; '/site/captcha'[/color], 'allowEmpty'=&gt; &#33;extension_loaded('gd'))

);

}

应该是你的actions对应的controller出现不统一导致的,这样指定后统一用 /site/captcha,可以把 daogouController 里的 actions()删掉

非常感谢,model修改为:

public function rules()

| {

| return array(

| array(‘verifyCode’, ‘captcha’,‘captchaAction’ => ‘/site/captcha’,‘allowEmpty’=> !extension_loaded(‘gd’))

| );

| }

的确可以,但是这个问题算不算是yii的bug呢?而且我还不明白,yii为什么要这样设置?

这当然不是BUG,关键是你显示图片那儿的action和validate时的actionn必须在同一controller下。

或许你可以通过阅读相关代码理解它,还有你在论坛贴的代码太难看了,怎么不用 [code] 标签标记起来。