Jump to content


Photo

Login Form With Codeigniter


  • Please log in to reply
2 replies to this topic

#1 booxvid

booxvid

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 30 September 2012 - 12:54 AM

Here's the snippet code in controller:

public function me_login(){

$this->load->view('header');
$this->form_validation->set_rules('username','First Name','required');
$this->form_validation->set_rules('password','Last Name','required');

$this->form_validation->set_error_delimiters('
','');

if($this->form_validation->run()==FALSE){
$this->load->view('view_login');
}
else{

$u= $this->input->post('username');
$p= $this->input->post('password');

$username = trim($u);
$password = trim($p);

//search same info from db
$boolean = $this->blog_model->login($username,$password);


if($boolean){

$userdata = array(
'username' => $username,
'logged_in' => TRUE
);

$this->session->set_userdata($userdata);

echo "";
$this->load->view('view_home',$userdata);

}
else{
echo "";
$this->load->view('view_login');
}
}


}

here's my blog_model where my login function located:
public function login($username,$password){
$encode = $this->encrypt->encode($password);
$query = $this->db->get_where('blog_user', array('username' => $username, 'securepass' => $encode));
return $query->result();

}

I don't know why my Login function will always be failed.

Edited by booxvid, 30 September 2012 - 12:59 AM.


#2 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 30 September 2012 - 07:25 PM

Foremost, I don't understand your usage with the 'error deliminators' - are you expecting to use newlines?

This:
$u= $this->input->post('username');
$p= $this->input->post('password');

$username = trim($u);
$password = trim($p);

Can be replaced with:
$this->form_validation->set_rules('username','First Name','required|trim');
$this->form_validation->set_rules('password','Last Name','required|trim');

Now, in order to determine why it's failing, I suggest you query for the username that matches the input value and echo out the password in its row AND the hashed POST value. If the two do not match, then you need to figure out where the problem starts.
?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#3 hell_yeah

hell_yeah

    Member

  • Members
  • PipPip
  • 11 posts

Posted 04 November 2012 - 02:55 AM

In addition to Mahngiel's suggestions, in your login function, since you only need to check if the user name is valid or not and not using the data, you can do something like this instead of returning the whole data



public function login($username,$password){
$encode = $this->encrypt->encode($password);
$query = $this->db->get_where('blog_user', array('username' => $username, 'securepass' => $encode));
if ($query->num_rows() == 1){
   return TRUE;
}else{
   return FALSE;
}

}







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com