2011-06-25 61 views
0

我正试图在表单中实现一个验证码。会话变量在Jquery发布的脚本中不可见?

共有三个文件:

html表单。

php captcha图像生成器。

php captcha检查器。

Everythiong工作正常,但我似乎无法检索到我的验证码检查脚本中的验证码脚本创建的Session变量。

在形式:

<img src="captcha/captcha.php"></img> 
<input type="text" id="CAPTCHA"><p id="caperror"></p> 
<p><input name="B1" type="submit" value="Submit" /> 
</form> 

<script> 
$('#contactform').submit(function() { 
    var cap = $('#CAPTCHA').val(); 
    cap = 'CAPTCHA=' + cap; 
    $.ajax({ 
     type: 'POST', 
     url: 'captcha/capcheck.php', 
     data: cap, 
     success: success, 
     dataType: "text", 
     error: postfail 
    }); 
}); 

function success(result) { 
    if (result == 'true') { 
     alert('Correct'); 
     return true; 
    } else { 
     alert(result); 
     return false; 
    } 
} 

function postfail() { 
    alert('post failed'); 
    return false; 
} 
</script> 

会话变量[ 'CAPTCHA'〕中captcha.php脚本中生成。

我尝试找回它,并检查IM的capcheck.php脚本与此:

<?php 
session_start(); 

if($SESSION['CAPTCHA']) { 
echo 'session='.$SESSION['CAPTCHA']; 
} 
if((isset($POST['CAPTCHA'])) && (isset($SESSION['CAPTCHA']))){ 
if($_SESSION['CAPTCHA'] != $_POST['CAPTCHA']) { 

    echo 'false'; 


    } else { 
    session_destroy(); 
     echo 'true'; 
} 

} 
else{ 
echo $_POST['CAPTCHA'].' '. $SESSION['CAPTCHA']; 
session_destroy(); 
} 
?> 

它始终是空的,我不知道为什么!我已经实现了没有jQuery,在不同的网站,它完美的作品。

Captcha.php:

<?php 

     session_start(); 

     /** 
     * Send PNG headers to the browser 
     **/ 
     header('content-type: image/png'); 

     $numbers = array(); 
     $numbers[] = rand(1, 5); 
     $numbers[] = rand(1, 5); 

     if(rand(0, 1) == 0) { 
      $_op      = '+'; 
      $code      = strval($numbers[0]) .' + ' .strval($numbers[1]) .' ='; 
      $_SESSION['CAPTCHA'] = $numbers[0] + $numbers[1]; 
     } else { 
      $_op      = '+'; 
      $code      = strval($numbers[0]) .' + ' .strval($numbers[1]) .' ='; 
      $_SESSION['CAPTCHA'] = $numbers[0] + $numbers[1]; 
     } 
     session_write_close(); 

     /* Our TTF font file, you may use others */ 
     $font = dirname(__FILE__) .'/fonts/arial.ttf'; 

     /* set the width */ 
     $width = 16; 
     //$width = (strlen($code) * $width) + 2; 
     $height = 24; 

     $code_length = 7; 

     $image_height = $height + 2; 
     $image_width = $width * $code_length + 20; 

     $im  = imagecreatetruecolor($image_width, $image_height); 
     $white = imagecolorallocate($im, 255, 255, 255); 
     imagefill($im, 0, 0, $white); 

     /* Some themes */ 
     $theme = array(); 
     $theme[] = array('CHAR_COLOR' => 
           array('R' => array(56, 21), 
             'G' => array(85, 25), 
             'B' => array(14, 195) 
             ), 
           'BG_COLOR' => 
           array('R' => array(12, 76), 
             'G' => array(54, 67), 
             'B' => array(26, 23) 
             ) 
          ); 
     $theme[] = array('CHAR_COLOR' => 
           array('R' => array(59, 72), 
             'G' => array(35, 55), 
             'B' => array(65, 63) 
             ), 
           'BG_COLOR' => 
           array('R' => array(30, 55), 
             'G' => array(70, 30), 
             'B' => array(70, 30) 
             ) 
          ); 
     $theme[] = array('CHAR_COLOR' => 
           array('R' => array(76, 25), 
             'G' => array(38, 50), 
             'B' => array(58, 78) 
             ), 
           'BG_COLOR' => 
           array('R' => array(94, 30), 
             'G' => array(97, 30), 
             'B' => array(30, 55) 
             ) 
          ); 

     $pos_x = 5; 
     $pos_y = 20; 
     $random = rand(0, (count($theme) - 1));/* Get a random theme */ 

     /** 
     * Place each character into the image 
     **/ 
     $angle = 0; 
     $size  = 16; 
     for($i = 0, $count = strlen($code); $i < $count; $i++) { 

      $color = imagecolorallocate($im, 
                rand($theme[$random]['CHAR_COLOR']['R'][0], $theme[$random]['CHAR_COLOR']['R'][1]), 
                rand($theme[$random]['CHAR_COLOR']['G'][0], $theme[$random]['CHAR_COLOR']['G'][1]), 
                rand($theme[$random]['CHAR_COLOR']['B'][0], $theme[$random]['CHAR_COLOR']['B'][1]) 
               );   

      imagettftext($im, $size, $angle, $pos_x, $pos_y, $color, $font, $code{$i}); 
      $pos_x += $width + 1; 

     } 

     /* Finally show image */ 
     imagepng($im); 
     imagedestroy($im); 
    ?> 
+0

请至少张贴captcha.php的相关部分。 – TNi

+0

发表captcha.php – Ben

+0

你的一些'$ POST'和'$ SESSION'变量缺少下划线。这是复制/粘贴错误还是? –

回答

1

你的一些$POST$SESSION变量缺少下划线。正如您在评论中提到的那样,这似乎是问题所在。

我建议在这种情况下使用带有适当语法突出显示支持PHP的文本编辑器,这样很容易识别这些错误。

+0

我现在正在使用记事本++ ...我喜欢Zend工作室,但是我的试用用完了,而且我的公司很便宜。我不认为notepad ++具有非常复杂的语法突出显示,并且我没有看到在该区域添加任何内容的插件。但我绝对同意你的看法,语法错误突出显示对我这样的简单错误是有帮助的。 – Ben