My users table:
- user_id PK
- username
- password
- 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?
Username or password are incorrect. - BEingprabhU