【问题标题】:jQuery ajax request creates new Code Igniter sessionjQuery ajax 请求创建新的 Code Igniter 会话
【发布时间】:2011-08-31 15:31:56
【问题描述】:

我正在使用 Codeigniter php 框架。

我的函数 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'];
    }

【问题讨论】:

  • codeigniter 实现了会话(库)并相应地启动会话
  • 能把generate_captcha()的出处发上来

标签: php jquery session codeigniter


【解决方案1】:

当它是ajax请求时,您可以在会话开始的地方(可能是父类)关闭新会话的创建。或者你可以把你的 generate_captcha() 放在 check_captcha() 之后。

【讨论】:

  • 首先发布到 check_captcha 函数
猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 2014-05-13
  • 2012-03-27
  • 1970-01-01
  • 2015-05-18
相关资源
最近更新 更多