12-01-2010, 06:39 AM
[eluser]Joures[/eluser]
I want to display an error message if a user has
entered the wrong username or password in a login system.
I made use of flashdata and i kinda got it to work except it's not displayin
the error on the first click off the submit button, but on the second. And then it
stays there until i've refreshed the page twice. Any ideas?
My code looks like this:
Model
Controller
View
I want to display an error message if a user has
entered the wrong username or password in a login system.
I made use of flashdata and i kinda got it to work except it's not displayin
the error on the first click off the submit button, but on the second. And then it
stays there until i've refreshed the page twice. Any ideas?
My code looks like this:
Model
Code:
class User extends Model {
function validate() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1) {
return true;
}
}
Controller
Code:
class Login extends Controller {
function index() {
$this->load->view('misc/login_form');
}
function validate_credentials() {
$query = $this->user->validate();
if($query) {
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('dashboard/index');
} else {
$message = '<div class="flash_error">Fel användarnamn eller lösenord.</div>';
$this->session->set_flashdata('message', $message);
$this->index();
}
}
View
Code:
<div class="box-content-25">
<?php echo $this->session->flashdata('message'); ?>
<?= form_open('login/validate_credentials'); ?>
<p><?= form_input('username', set_value('username', 'Användarnamn')); ?></p>
<p><?= form_password('password', 'lösenord'); ?></p>
<?= form_submit('submit', 'Logga in'); ?>
</div>