Jump to content

Archived

This topic is now archived and is closed to further replies.

booxvid

Login Form With Codeigniter

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;
}

}


Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.