Welcome Guest, Not a member yet? Register   Sign In
Loosing Session value - Captcha plugin
#1

[eluser]Martin Rusev[/eluser]
Hi all,

I am creating small registration form, using Codeigniter captcha plugin, but instead of using database I tried the same approach with sessions. The session data is not available everywhere in the controller and I can't resolve this problem.

Code:
function index()
    {
        $rules['captcha'] ="required|callback_captcha_check";
        
        $this->validation->set_rules($rules);
        
        
        $fields['captcha'] = 'Secret word';
            
        $this->validation->set_fields($fields);

                //Not working here
                print("outer test: " . $this->session->userdata('captcha_word'). "<br>") ;
        if ($this->validation->run() == FALSE)
        {
        $captcha = $this->generate_captcha();    
        $data['cap'] = $captcha['image'];
        
        // Session is working here
        print("inner test: " . $this->session->userdata('captcha_word'). "<br>");
    
        $this->load->view('registration', $data);
        }
        else
        {
                 // some code
                }

      // GENERATING CAPTCHA - EVERYTHING IS WORKING FINE HERE
      function generate_captcha()
      {
        $this->session->unset_userdata('captcha_word');
        
        $word = array('test', 'test1', 'test2','test3');
        $word_scrobble = array_rand($word);
        $vals = array(
                    'word'         => $word[$word_scrobble],
                    'img_path'     => BASEPATH. 'upload' ,
                    'img_url'     => base_url() . 'public/upload/',
                    'font_path'     => BASEPATH . 'fonts/font.ttf',
                    'img_width'     => '120',
                    'img_height' => 30,
                    'expiration' => 3600
                );

        $captcha = create_captcha($vals);
        
        $this->session->set_userdata(array('captcha_word' => $captcha['word']));    

        return $captcha;
        
       }
  
         //CHECKING CAPTCHA VALUE - DOESN'T WORK, it needs that Session value :)  
         function captcha_check()
        {
                    // RETURNS FALSE  
            var_dump($this->session->userdata('captcha_word'));
            
        }
#2

[eluser]Martin Rusev[/eluser]
I replaced CI_Session with Native Session library and now everything works just fine :-)




Theme © iAndrew 2016 - Forum software by © MyBB