CodeIgniter Forums
Cannot login in my site - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Cannot login in my site (/showthread.php?tid=55319)



Cannot login in my site - El Forum - 10-20-2012

[eluser]jim04[/eluser]
Hello,

I 'm new in CI and and i study according to some video tutorials. Even if the topic is similar to others the answers couldn't help me, so i decided to post it again to solve the problem

I have this Controller

Code:
<?php

class Login extends CI_Controller{

function index(){
  $data['main_content'] = 'login_form';
  $this->load->view('includes/template', $data);
}

function validate_credentials(){
  $this->load->model('membership_model');
  $query = $this->membership_model->validate();

  if($this->membership_model->validate()){
   $data = array(
   'username' => $this->input->post('username'),
   'is_logged_in' => true
   );
   $this->session->set_userdata($data);
            
   redirect('site/members_area');

  }
  
  else{
   $this->index();

  }
  
}

and this model

Code:
<?php

class Membership_model extends CI_Model{

function validate(){  
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('membership');

  if ($query->num_rows == 1){
   return true;
  }  
}

}


when i try to login it takes me to tis URL http://localhost/ci_day6/index.php/login/validate_credentials either the username and password are true or not.

Please help me!


Cannot login in my site - El Forum - 10-20-2012

[eluser]benton.snyder[/eluser]
It looks like you should be redirected on a successful login attempt to index.php/site/memebers_area and that you'll stay at index.php/login/validate_credentials upon failure. Sounds like the login attempt is failing. Might be able to help more if you could post your membership table structure and the record that you are trying to match.

Also, the following line is extraneous and should be removed (You call the function twice when it need only be called once.):
Code:
$query = $this->membership_model->validate();



Cannot login in my site - El Forum - 10-20-2012

[eluser]egy_programozo[/eluser]
pls change the model to this:
Code:
<?php

class Membership_model extends CI_Model{

function validate(){  
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('membership');
  if ( $query ){ // correct query
    if ($query->num_rows == 1){
      return true;
    }
  }else{
    //query error
  }
  return false; // default return
}

}

That's not solve the problem, but recommended.

Try
Code:
$this->db->last_query();



Cannot login in my site - El Forum - 10-21-2012

[eluser]jim04[/eluser]
Unfortunatelly what you suggested isn't working. But i made something that made it work.
In membership_model i typed
Code:
$this->db->where('password', $this->input->post('password'));

instead of
Code:
$this->db->where('password', md5($this->input->post('password')));
.

In config file i typed
Code:
$config['encryption_key'] = 'md5';
but i can see the password in my database. Is it right?
What else can you recommend?


Cannot login in my site - El Forum - 10-21-2012

[eluser]benton.snyder[/eluser]
CI's $config['encryption_key'] will not handle hashing passwords for insertion into the database. Either you need to handle the hashing yourself (such as manually md5()), or you could use an external library to handle the authentication stuff. Personally, I recommend IonAuth.




Cannot login in my site - El Forum - 10-21-2012

[eluser]jim04[/eluser]
Could you give me more details?


Cannot login in my site - El Forum - 10-21-2012

[eluser]benton.snyder[/eluser]
$config['encryption_key'] is used by CI's encrypt library: http://ellislab.com/codeigniter/user-guide/libraries/encryption.html

While you could in theory use this encode() password's, it is generally recommended that passwords be one-way-hashed. You can't decode() a one-way-hashed password, there is no unmd5() for instance. If you did want to use CI's encrypt library to encode your passwords, you would still need to explicitly call the encode() function. Again, I do not recommend this in the slightest.

If you want your login/authentication system to be truly secured and robust, please look at http://benedmunds.com/ion_auth/


Cannot login in my site - El Forum - 10-22-2012

[eluser]jim04[/eluser]
@benton.snyder: I did what I had to do but it sends me an errror that the library can't be loaded. I typed it in config.php


@luongthanhtruyen: I think that the file you have to do is this view
Code:
<div id='login_form'>  &lt;!--Creates the form--&gt;

<h1>Login</h1>

&lt;?php
echo form_open('login/validate_credentials');
echo form_input('username', 'Enter your username');
echo form_password('password', 'Enter your password');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Create new account');
?&gt;

</div>

Finally i used your code above but something weird happened, it took me to http://localhost/ci_day6/index.php/login/validate_credentials


Cannot login in my site - El Forum - 10-22-2012

[eluser]jim04[/eluser]
No, not at a lot
it takes me to http://localhost/ci_day6/index.php/login/validate_credentials

I just wanted to thank you for helping me.