Sunday, December 31, 2017

Does not login into account at first time in Codeigniter 3 framework

Leave a Comment

I have a website and login system. In localhost everything is perfect. However, in the server (HOSTGATOR), when people want to log in their account, they type login password and hit "Login" button, it refreshes the page but does not log in. They should enter 2-3 times and then systems logs in. Where can be the problem?

public function __construct() {     parent::__construct();     $this->load->helper(array('form','url', 'security'));     $this->load->library(array('session', 'form_validation', 'email'));     $this->load->database();     $this->load->model('User_model'); }   public function login(){             $data['title'] = 'Sign In';          $validator = array('success' => false, 'messages' => array());          $validate_data = array(             array(                 'field' => 'usernameforlog',                 'label' => lang('controller-username-label'),                 'rules' => 'trim|required|alpha_dash'             ),             array(                 'field' => 'passwordforlog',                 'label' => lang('controller-password-label'),                 'rules' => 'trim|required|md5'             )         );          $this->form_validation->set_rules($validate_data);         $this->form_validation->set_message('required', lang("form_validation_required"));           $this->form_validation->set_message('alpha_dash', lang("form_validation_alpha_dash"));         $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');                if ($this->form_validation->run() === FALSE)             {                  // fails                 $validator['success'] = false;                 foreach ($_POST as $key => $value) {                     $validator['messages'][$key] = form_error($key);                 }                  log_message('error', 'validation errors' );              }else {                  // Get username                 $username = mb_strtolower($this->input->post('usernameforlog'), 'UTF-8');                 // Get and encrypt the password                 $password = $this->input->post('passwordforlog');                 // Login user                  $user_id = $this->user_model->login($username, $password);                 if($user_id){                     // Create session                      $user_data = array(                      'id' => $user_id,                      'instructors_slug' => $username,                      'logged_in' => true                      );                      $this->session->set_userdata($user_data);                     $validator['success'] = true;                     $validator['messages'] = array();                 } else {                      $validator['success'] = false;                     $validator['messages'] = '<div class="alert alert-danger text-center">'.lang('controller-user-email-error3').'</div>';                 }                    }              echo json_encode($validator);         } 

Here is the ajax file:

$(document).ready(function() {     $("#loginform").unbind('submit').bind('submit', function() {         var form = $(this);          $.ajax({             url: form.attr('action'),             type: form.attr('method'),             data: form.serialize(),             dataType: 'json',             success:function(response) {             console.log(response);                               if(response.success) {                     //redirect main page                     location.reload();                 }                 else {                     $("#loginmsg").html(response.messages);                      $.each(response.messages, function(index, value) {                         var element = $("#"+index);                          $(element)                         .closest('.form-group')                         .removeClass('has-error')                         .removeClass('has-success')                         .addClass(value.length > 0 ? 'has-error' : 'has-success')                         .find('.text-danger').remove();                          $(element).after(value);                      });                 }             } // /success         });  // /ajax          return false;     }); }); 

1 Answers

Answers 1

because location.reload() will reload your login() page, in here you need to add method for checking if user has logged_in ?

public function login() {      $logged_in = $this->session->userdata('logged_in');      if($logged_in)     {         // example redirect to dashboard         redirect('dashboard', 'refresh');     }       // rest of code } 

instead just echo json_encode($validator); better set also your content header as application/json

return $this->output->set_content_type('application/json')->set_output(json_encode($this->response)); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment