I have a login form that I’m having trouble getting errors to post properly.
I have a form that has a email and password field. When I submit the form blank, both “invalid email/password” errors post as they should, however with any other combination ( correct email and incorrect password, both incorrect ) the “invalid email” post no matter what and the “invalid password” NEVER post’s. Here is my code:
function validate_credentials_login() { // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
// CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) { $this->session->set_userdata(array( 'email' => $this->input->post('email_login') )); redirect('account/dashboard'); exit; } } }
okay, so with this current set-up. I’m getting both errors no matter what…meaning when I have a correct email and a false password, both errors pose, as only the password one should. It’s saying valid emails are invalid.
Here is what I currently have:
function validate_credentials_login() { // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
// CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) { $this->session->set_userdata(array( 'email' => $this->input->post('email_login') )); redirect('account/dashboard'); exit; } } }
2. You are catching “email” as $this->input->post(‘email_login’), same as previous.
3. I am not sure if that could be problematic but try to structure the run validation method before all the stuff, like this:
function validate_credentials_login() { // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
The way I have it set up now, is that it doesn’t pose the email and password error from the OTHER form on the page ( which is good and was a problem before ) and poses both errors properly if submitted blank BUT if correct email and INCORRECT password, then it just loads the page all white ( the url at this point is what it should be “http://www.clci.dev/index.php/auth/validate_credentials_login” )
Here is what I have:
function validate_credentials_login() { // WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
// CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) { $this->session->set_userdata(array( 'email' => $this->input->post('email_login') )); redirect('account/edit'); exit;
Ok - First off I want to thank you for all of your time in this matter, and secondly I’m getting sadder by the minute because I’m so close and just not understanding what the problem is.
I have taken your advice on restructuring based off of the user guide, however some errors are posting as they should:
when you submit the form with out touching any fields, it runs the correct errors, and when you enter a correct email and don’t touch pw field, it runs the correct error but when you put in incorrect information with any other incorrect information it loads a white page
I have used the profiler on those pages and the query is running correctly and the fields are being populated correctly and I can log in with out a problem when the correct info is sent.
// CHECK THE USER'S PASSWORD AGAINST THE ONE FROM THE LOGIN FORM if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login')))) { $this->session->set_userdata(array( 'email' => $this->input->post('email_login') )); redirect('account/edit'); exit;
since that last post, I’ve gone ahead and put the ->run() INSIDE of the $if($login) which makes more sense, however everything still seems to be sensitive to whether or not the input fields are filled ( they error correctly if not touched ) and getting these NEW errors on an all white page:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: libraries/Form_validation.php
Line Number: 953
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /Users/michaelsanger/Sites/cl_ci_new/system/core/Exceptions.php:185)
Filename: core/Common.php
Line Number: 442
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE ` = ‘RalphLeMouf@gmail.com’ LIMIT 1’ at line 2
SELECT * WHERE ` = ‘RalphLeMouf@gmail.com’ LIMIT 1
So I’ve restructured my logic scheme a bit and heeded all of your advice and STILL having trouble getting this to work correctly. I’ve gone and commented out every step Im’ doing so you can see the logic that I THINK I am using.
As far as the said “elses” what I have seems to account for everything but for some reason it is not.
function validate_credentials_login() { // LOAD THE SESSION LIBRARY $this->load->library('session'); // LOAD THE URL AND FORM HELPERS $this->load->helper(array('form','url')); // LOAD THE RELEVENT MODEL AND SET A NAME FOR IT $this->load->model('user_model', 'um'); // LOAD ENCRYTPION LIBRARY IN ORDER TO ENCRYPT PASSWORDS PROPERLY $this->load->library('encrypt'); // LOAD THE FORM VALIDATION LIBARARY TO MAKE USE OF ERROR HANDLING $this->load->library('form_validation'); // SET RULES FOR MY EMAIL FIELD $this->form_validation->set_rules('email_login', 'Email', 'trim|required'); // SET RULES FOR MY PASSWORD FIELD $this->form_validation->set_rules('password_login', 'Password', 'trim|required'); // MAKE A VARIABLE FOR MY SUBMIT BUTTON $login = $this->input->post('submit_login');
// IF THE SUBMIT BUTTON IS SET if($login) {
// MAKE THIS VARIABLE THAT CHECKS THE EMAIL FEILD INSERTED VIA POST AGAINST THE ONE STORED IN MY DATABASE $user = $this->um->validate_home_login(array('email' => $this->input->post('email_login')));
// IF THIS USER EXISTS AND THERE ARE NO ERRORS SET OFF BY THE FORM VALIDATION CHECK if($user && $this->form_validation->run()) { // DO THIS STUFF AKA IF THE USERS PASSWORD IS THE SAME AS THE ONE INSERTED VIA POST AND THE USERS EMAIL IS THE SAME INSERTED VIA POST EVERYTHING IS GOOD AND YOU CAN LOG THEM IN AND START A SESSION if($user->password == $this->encrypt->sha1( $user->salt . $this->encrypt->sha1($this->input->post('password_login'))) && $user->email == $this->input->post('email_login')) { $this->session->set_userdata(array( 'email' => $this->input->post('email_login') )); redirect('account/edit'); } $data['main_content'] = 'home/home_page'; $this->load->view('includes/templates/home_page_template', $data); }
// IF ANYTHING IS OFF OR DOESN'T MATCH ( SUPOSEDELY ) RUN THE FORM VALIDATION AS FALSE AND RELAOD THE PAGE WITH ERRORS elseif($this->form_validation->run() == FALSE) { $data['main_content'] = 'home/home_page'; $this->load->view('includes/templates/home_page_template', $data); } } }
function validate_home_login($data) { // TAKING THE DATA FROM THE MODEL AND CHECKING IT AGAINST THE STORED INFO IN THE DB $query = $this->db->where($data)->get('users', '1');