CodeIgniter Forums
Kcaptcha session vars not being set - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Kcaptcha session vars not being set (/showthread.php?tid=20933)



Kcaptcha session vars not being set - El Forum - 07-26-2009

[eluser]bmgz[/eluser]
I am trying to use Kcaptcha with codeigniter. I created a function in the controller to output the image but the session var is not being set afterwards:

controller:

Code:
function captcha(){
    $this->load->library(array('session', 'kcaptcha/kcaptcha'));
    $this->session->set_userdata('captcha_keystring', $this->kcaptcha->getKeyString());
}

view:

Code:
<img src="quiz/captcha" />

When I disable the image headers and image output on the kcaptcha class, then the captcha session var is set, but when they are enabled the session variable is not set?


this is the tail of the Kcaptcha class..

Code:
...
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', FALSE);
        header('Pragma: no-cache');
        
        if(function_exists("imagejpeg")){
            header("Content-Type: image/jpeg");
            imagejpeg($img2, null, $jpeg_quality);
        }else if(function_exists("imagegif")){
            header("Content-Type: image/gif");
            imagegif($img2);
        }else if(function_exists("imagepng")){
            header("Content-Type: image/x-png");
            imagepng($img2);
        }
        
    
    }

    // returns keystring
    function getKeyString(){
        return $this->keystring;
    }



Kcaptcha session vars not being set - El Forum - 07-26-2009

[eluser]Derek Allard[/eluser]
Sessions won't get set until the next page load. I assume you have a user enter the captcha and then submit a form? The session should be available on the next page.

If you want to take kcaptcha out of the equation, we could get this running pretty quick for you I'm sure. How are you having the user leave the page they are on and arrive at the new page?


Kcaptcha session vars not being set - El Forum - 07-26-2009

[eluser]bmgz[/eluser]
...
[quote author="Derek Allard" date="1248626274"]How are you having the user leave the page they are on and arrive at the new page?[/quote]
...

I have a form and a captcha, which posts to a function that validates the form and inserts the record upon success OR loads the form view again...

I reckon I should probably use one of the existing CI captcha libs...


Kcaptcha session vars not being set - El Forum - 07-26-2009

[eluser]Derek Allard[/eluser]
Right, but does this happen by directing them to a new page, or is the session set and then the logic of record insertion happen in the same spot?

I'm trying to eliminate the fact that sessions won't "stick" until the next page load as a cause.


Kcaptcha session vars not being set - El Forum - 07-26-2009

[eluser]bmgz[/eluser]
I performed the following in the save() function in the controller...

Code:
if($this->session->userdata('captcha_keystring') != $this->input->post('captcha')){
            echo $this->input->post('captcha').'<br />';
            echo $this->session->userdata('captcha_keystring').'<br />';
            echo 'fail!';
            exit;
        }else{
            echo $this->input->post('captcha').'<br />';
            echo $this->session->userdata('captcha_keystring').'<br />';
            echo 'pass!';
            exit;
        }

it seems the problem rests solely on $this->session->userdata('captcha_keystring') not being set.


Kcaptcha session vars not being set - El Forum - 07-26-2009

[eluser]bmgz[/eluser]
although I have clearly stated that it must be set:

Code:
function captcha(){
    $this->load->library(array('session', 'kcaptcha/kcaptcha'));
    $this->session->set_userdata('captcha_keystring', $this->kcaptcha->getKeyString()); // LOOK HERE???
}

I think there is a bug or something, as far as i can make out, when I include a library, CI automatically creates an instance of it? in this case the Kcaptcha class automatically spits out a JPEG with an image header etc.. I don't see how that would interrupt the flow on the php script on the server-side, the line after load->library should still be called, so something is wrong with the last line of code above..???