开发者

form validation question

I have a question about form validation in CodeIgniter:

<? form_open('user/check_login' ?>
  <fieldset id="login_fields">
     <label for="login">Login</label>
          <input name="login" type="text" class="textfield" id="login" />
     <label for="password">Password</label>
          <input name="password" type="password" class="textfield" id="password" />
     <input type="submit" name="Submit" value="Login" />
  </fieldset>
</form>

As the you can see above, the form is submitted to check_login method in user controller.

After a certain number of unsuccessful login attempts, I would include another input field (captcha) in the form. For example

<? if ($attempts > 3) { ?>
<label for="captcha">captcha</label>
     开发者_如何学C     <input name="captcha" type="text" class="textfield" id="captcha" />
<? } ?>

But the problem is that when the extra field is added, and the form is submitted then, how the check_login method will know that this time captcha field was added in the form, and that can not be left blank.

Thanks.


You're either need to use the CI session library or the PHP $_SESSION array. Personally, I would use flash_data in the session lib.

$this->load->library('session');
$attempts = $this->session->flashdata('fails');
if(!is_numeric($attempts)) $attempts = 0;
else $attempts++;
$this->session->set_flashdata('fails', $attempts);

At that point, you can just pass $attempts to your view and you should be good.


isset( $_POST['captcha'] )

or

$this->input->post('captcha') not equals to FALSE

if any of this conditions are meet, the add the rule to the form_validation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜