Welcome Guest, Not a member yet? Register   Sign In
Cant store session data - worked with flashdata but no longer works at all
#1

[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="&lt;?php echo site_url('captcha/captcha/generatecaptcha'); ?&gt;" alt="" />
&lt;input name="ccode" id="ccode" type="hidden" value="" /&gt;&lt;/div>

and the corresponding controller is:-

Code:
&lt;?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
  }

}

?&gt;

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?
#2

[eluser]CroNiX[/eluser]
Check your url in your img src, you have captcha 2x. Are you even getting the image?
captcha/captcha/generatecaptcha
#3

[eluser]Dandy_andy[/eluser]
Yes i am. The 2x captcha is because of the folder structure! The image works fine and everything was working to plan until I implemented the jquery form validation.

For some reason, having run a test, I cannot save session data from this controller and I'm puzzled as to why. It seems to save flashdata but this is no good to me now as the jquery axaj validator sends lots of server requests before a user has had a chance to submit the form so the flashdata is useless. Any ideas what the issue could be? I'm really puzzled with this...
#4

[eluser]Dandy_andy[/eluser]
Interestingly, the verify function isn't returning ANY session data variable including the CI session data. I can't figure out why. Session data is being created and I have checked this in the cookies but this function isn't returning the data even though it was before...
#5

[eluser]Dandy_andy[/eluser]
Figured it out! Phew! For anyone who wants to know and who maybe experiencing the same issue, it appears that the imagedestroy() function is flashing away the session data. By moving the set userdata as shown below, this eliminates the problem:-

Code:
&lt;?php

class Captcha extends CI_Controller {

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");
$this->session->set_userdata('cd', $string); //set temporary session string to be verified against
imagepng($image);
imagedestroy($image);

  }

}
?&gt;

Problem solved!





Theme © iAndrew 2016 - Forum software by © MyBB