[eluser]Dandy_andy[/eluser]
I am generating a CAPTCHA string and would like to save the 'realcode' as session data. This worked as flashdata initially, but now that I have created a realtime jquery form validator, I cant save any session data from the controller generating the CAPTCHA.
The HTML code in the view calls up the CAPTCHA as an image:-
Code:
!--- Display captcha --->
<div class="controls"> <img src="<?php echo site_url('captcha/captcha/generatecaptcha'); ?>" alt="" />
<input name="ccode" id="ccode" type="hidden" value="" /></div>
and the corresponding controller is:-
Code:
<?php
class Captcha extends CI_Controller {
public function generatecaptcha(){
$string = '';
for ($i = 0; $i < 8; $i++) {
$string .= chr(rand(97, 122));
}
$code['rand_code'] = $string;
$dir = '';
$image = imagecreatetruecolor(200, 60);
$color = imagecolorallocate($image, 200, 100, 90); // red
$backgrd = imagecolorallocate($image, 255, 204, 255); // pink
imagefilledrectangle($image,0,0,200,100,$backgrd);
imagettftext($image, 30, 5, 10, 40, $color, $dir."font.ttf", $code['rand_code']);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
$this->session->set_userdata('sd', $code['rand_code']); //set temporary session string to be verified against
}
}
?>
The intention here is to store the 'rand_code' data to compare it with the user input later as there are form validation processes before this is done and this is the only way I can think of doing this as it's seemingly impossible otherwise. I don't want to store the data in the DB and I really shouldn't have to as this system did work before form validation.
Now in my registration controller, I want to access the session data which I am trying to do as follows (function is just written for testing purposes only):-
Code:
public function verify() {
$realcode = $this->session->userdata('sd');
$usercode = $this->input->post('verify'); // Set username value from post
echo "this is the real code: ".$realcode;
echo "this is what I entered code: ".$usercode;
exit();
//verify username
}
But this doesn't return any value for $realcode. This worked fine before I used a POST jquery form validation and I think session data is somehow no longer being stored - or am I missing something here?