Login script with validation - 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: Login script with validation (/showthread.php?tid=2611) |
Login script with validation - El Forum - 08-14-2007 [eluser]codex[/eluser] Yeah, I know that there's a few scripts out there, but I want to understand how and why things work like they do, just for a better overall understanding. Imagine a homepage with a small loginbox (username, password, submit). The data needs to be checked against a database. You know the drill. Using validation and callback, how would you go about? I've made a callback for the username, but there seems to be no way to send the password for checking also. Should I make 2 seperate callbacks instead of trying to do it in one? Is it even possible? da rules: Code: $rules['user_name'] = "required|callback_check_logindata"; da callback: Code: function check_logindata($username, $password) Login script with validation - El Forum - 08-14-2007 [eluser]Michael Wales[/eluser] You can access any field from the form if you have assigned the fields names using $this->validation->[fieldname]: Code: $rules['username'] = 'trim|required|callback__check_logindata'; Code: function _check_logindata($username) { Note: I also changed your callback function by prepending an underscore - now it won't be accessible via the URL - without that underscore people could access it via controller/check_logindata Login script with validation - El Forum - 08-14-2007 [eluser]codex[/eluser] [quote author="walesmd" date="1187140896"]You can access any field from the form if you have assigned the fields names using $this->validation->[fieldname]: Code: $rules['username'] = 'trim|required|callback__check_logindata'; Code: function _check_logindata($username) { Note: I also changed your callback function by prepending an underscore - now it won't be accessible via the URL - without that underscore people could access it via controller/check_logindata[/quote] Hey thanks! I was just about to give up on CI ;-) I've tried it, but it doesn't work properly. The required rules apply, but even with a false username you're being redirected. Maybe the model is no good? Model: Code: <?php Controller: Code: <?php View: Code: <?php echo form_open();?> Login script with validation - El Forum - 08-14-2007 [eluser]codex[/eluser] Ah, this bit had something to do with it: Code: if ($this->login_model->check_login_data($username, $password) !== FALSE) { Should be: Code: if ($this->login_model->check_login_data($username, $password) == FALSE) { But now I get a different error when making an intended error: Code: Unable to access an error message corresponding to your field name. :roll: Login script with validation - El Forum - 08-14-2007 [eluser]codex[/eluser] Of course, 'check_logindata' should be '_check_logindata', I'm blind ;-) Login script with validation - El Forum - 08-14-2007 [eluser]Michael Wales[/eluser] Yeah sorry - I didn't really look over the code at all to ensure it was logically correct - was just shooting for the basic idea on that one. Login script with validation - El Forum - 08-14-2007 [eluser]codex[/eluser] Nahh man, I'm glad you could help out. Works like a charm! And I've learned something ;-) |