2011-08-31 111 views
0

我正在使用Codeigniter php框架。jQuery ajax請求創建新的代碼點火器會話

我的功能newRecipe()創建一個帶驗證碼的表單。
我的功能generate_captcha()創建驗證碼並將其保存在用戶會話中。

當用戶提交表單時,會進行一些驗證,然後使用jQuery ajax請求將輸入的驗證碼發送到服務器,我希望將客戶端的驗證碼與用戶會話中的驗證碼進行比較。

但是,當在check_captcha()中檢查驗證碼時,會創建一個新會話,並且我無法將來自客戶端的驗證碼與舊會話的驗證碼進行比較。

是否有可能停止創建新會話的jQuery ajax請求?

function newRecipe(){ 
     $this->load->library('session'); 
     $this->generate_captcha(); 

     $data = array(
      'view' => 'newRecipe', 
      'captcha' => $this->session->userdata('captcha') 
     ); 

     $this->load->view('includes/template',$data); 
    } 
    function check_captcha(){ 
     $this->load->library('session');  
     $captcha_order = $_POST['captcha_order']; 
     $order = $this->session->userdata('order'); 

     if(($captcha_order != $order)){ 
      $new_captcha = $this->generate_captcha(); 
      $comparison = false; 
     }else{ 
      $comparison = true; 
      $new_captcha = ''; 
     } 
     echo json_encode(array('comparison'=>$comparison,'newCaptcha'=>$new_captcha)); 
    } 

    on client side i have jquery: 

    function validate_form(){ 
     /*form validation*/ 
     captcha_pass(); 
    } 

    function captcha_pass(){ 
     var captcha_order = getCaptcha(); 
     $('.checkCaptcha').show(); 
     $.post(location+'dish/check_captcha',{captcha_order: captcha_order}, function(data) { 
         if(data.comparison == false){ 
          $('.captcha_wrap').html(data.newCaptcha); 
         }else{ 
          $("#new_dish_form").submit(); 
         } 
     },'json'); 
    } 

function generate_captcha(){ 
     $this->load->library('session'); 
     $this->load->library('captcha'); 
     if($this->session->userdata('captcha')) { 
      //delete captcha images 
      $this->captcha->deleteImages($this->session->userdata('order')); 
     } 

     $captcha = $this->captcha->generateCaptcha(); 
     $userCaptcha = array(
      'captcha' => $captcha['captcha'], 
      'order'  => $captcha['order'], 
      'formCreate' => time() 
     ); 
     $this->session->set_userdata($userCaptcha); 
     return $captcha['captcha']; 
    } 
+0

codeigniter有它的執行會話(庫),它開始會話相應 – Gasper

+0

您可以請發佈'generate_captcha()' – Hailwood

+0

源我發佈generate_captcha() – Gasper

回答

0

您可以在會話開始的地方(可能是父類)關閉創建新會話,當它是ajax請求時。或者你可以把你的generate_captcha()放在check_captcha()之後。

+0

post_check_captcha函數首先 – Gasper