1
votes

My users table:

  1. user_id PK
  2. username
  3. password
  4. date_created

View:

<div class="container">
    <div class="row">
    <div class="col-md-3">
    </div>  
    <div class="col-md-6">
    <?php
    $message = $this->session->flashdata('message');
    $form_error = $this->session->flashdata('error');
    ?>
    <div class="lg-md-2" align="center"><?php if(isset($message)){ echo $message; }?></div>
        <form method="post" action="<?php echo base_url();?>account/login">
          <div class="form-group">
            <label for="username">Username</label>
            <input class="form-control" id="username" name="username" type="text">
            <div id="form_error"><?php echo $form_error['username']; ?></div>
          </div>
          <div class="form-group">
            <label for="Password">Password</label>
            <input class="form-control" id="password" name="password" type="password">
            <div id="form_error"><?php echo $form_error['password']; ?></div>
          </div>
          <button type="submit" class="btn btn-primary btn-block">Submit</button>
        </form>
        <div class="text-center">
          <a class="d-block small mt-3" href="<?php echo base_url();?>pages/register_user">Register an Account</a>
        </div>       
    </div>
    <div class="col-md-3">
    </div>
    </div>

</div>



</body>

Controller:

public function login(){
    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if($this->form_validation->run() == FALSE){
    $form_error = array('username' => form_error('username'),
                    'password' => form_error('password'));
    $this->session->set_flashdata('error',$form_error);
    redirect('pages/login_user');
    }else{
    $data = array('username' => $this->input->post('username'),
            'password' => $this->input->post('password'));
    $result = $this->account_model->login_account($data);
    if($result  ){
    $new_session = array('username' => $result,
                        'is_logged_in' => TRUE);    
    $this->session->set_userdata($new_session);

    //redirect('pages/view_home');
    }else{
    $message = $this->session->set_flashdata('message', 'Incorrect Password');
    redirect('pages/login_user');
    }}
    }

Model:

public function login_account($data){
$query = $this->db->get_where('users', array('username' => $data['username']))->result();
if($query){
foreach($query as $row){
if(password_verify($data['password'], $row->password)){
return $row->username;}else{ return FALSE;}}
}else{ return FALSE; }
}

My question is, I wanted to be specific when the function fails if its incorrect password or if the username does not exist.

In my model, both scenario i returned FALSE. or do i have to return string? as in, return 'username' for invalid username and return 'password' for incorrect password and simply change the if statement in my controller to match that and send the appropriate message.

Or is there a better way? I keep getting reminded of try catch but i haven't really used that so if its that a better way then i'll do it but can someone post a code for it here as a reference.

Or is it simply best not to be specific about it and just input the message as 'invalid username/password' for both instances and let the user figure it out?

1
It's not correct to hint the user specifically, that his username or the password is incorrect. If you refer famous websites like google or facebook, they won't tell you which one is incorrect. So try to show error as Username or password are incorrect. - BEingprabhU
If you write that as an answer i will accept it. I'm relearning PHP/codeigniter on my own after 4 years of not touching programming for personal reasons. So i get really indecisive to the littlest things. I want to learn but i also want my standards to atleast be near the current standard as i already wasted 4 years. - Chief Makii
Re: try/catch. Exceptions should only be used in exceptional circumstances and should not be used to control the flow of logic. - DFriend
I can understand buddy. Even if you go untouched for just 2-3 months that is sufficient to forget the things. - BEingprabhU

1 Answers

0
votes

It's not correct to hint the user specifically, that his username or the password is incorrect. If you refer famous websites like google or facebook, they won't tell you which one is incorrect. So try to show error as Username or password are incorrect.