Welcome Guest, Not a member yet? Register   Sign In
User Functions
#1

[eluser]jzmwebdevelopement[/eluser]
Hello,

I have not used CI or PHP for a few months and I now have to develop a system. The below MVC is pretty crap and I was wanting to know how I could improve my code and I also want to include checking if the username or password is incorrect currently checks both and I would like to implement an add user feature along with SHA/Hashing of the passwords.

I currently have the following DB structure:

users:
id -> int 11 auto pri
fname -> varchar 50
lname -> varchar 50
phone -> int 45
email -> varchar 100
username -> varchar 50
password -> varchar 100
role -> varchar 5

I have tried to google but there is so many different ways of doing this and me myself know that there is a shorter way of producing the code.

Controller:

Code:
public function login() {
  
     $this->form_validation->set_rules('username','Username', 'required|valid_email|trim|max_length[99]|xss_clean');
     $this->form_validation->set_rules('password','Password', 'required|trim|max_length[200]|xss_clean|callback__checkUsernamePassword');

     if($this->form_validation->run() === TRUE) {
  // set CLEAN data in the session.
         redirect('admin/dashboard');
     }

  $this->index();
}

function logout() {
  
  $this->session->sess_destroy();
  $this->index();
  
}
  function _checkUsernamePassword() {
  // adding the _ makes the function 'private' so it can't be called from the URI.
  
         extract($_POST); // Gets data from form and creates vars
  
         $user = $this->login_model->check_login($username,$password);

         if(! $user){ // != If username or password are not correct
             $this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
             $this->form_validation->set_message('_checkUsernamePassword', 'Sorry %s is not correct.');
             return FALSE;

         } else {
          $this->session->set_userdata('logged_in',TRUE);
          $this->session->set_userdata('user_id',$user->id);
          $this->session->set_userdata('user_name',$user->first_name);
          $this->session->set_userdata('user_email',$user->email);
    return TRUE;
  
   }

Model:

Code:
function check_login($username,$password) {
  
  $query = $this->db->query("SELECT id, first_name, last_name, email, password FROM users WHERE email = ? and password = ?", array($username, md5($password))); // Result
  
  return ($query->num_rows() == 1) ? $query->row() : FALSE;
  
}
#2

[eluser]Bhashkar Yadav[/eluser]
you can create a custom library file like Validate.php in which you can validate all the user fields values and return if errors.




Theme © iAndrew 2016 - Forum software by © MyBB