[eluser]Carmichael[/eluser]
Example #1
Code:
function login()
{
$data['title'] = "Login";
if ($this->session->userdata('logged_in') == true)
{
redirect('/');
}
// validation form input
$this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if ($this->form_validation->run() == TRUE)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->Auth_model->login($username, $password);
if ($result == TRUE)
{
// login...
$data['message'] = $this->session->set_flashdata('message', 'Du är nu inloggad!');
redirect('/');
}
else
{
// login fail...
$data['message'] = $this->session->set_flashdata('message', 'Felaktiga uppgifter!');
redirect('login');
}
}
else
{
// display the login user form
// and set flashdata to show errors if there's
$data['message'] = (validation_errors()) ? validation_errors() : '';
$data['username'] = array(
'type' => 'text',
'name' => 'username',
'id' => 'username'
);
$data['password'] = array(
'type' => 'password',
'name' => 'password',
'id' => 'password'
);
$this->load->view('login', $data);
}
}
Login view
Code:
<?php echo form_open('login') ?>
<h1>Logga in</h1>
<?php echo $message; ?>
<p><label for="username"><b>Användarnamn:</b></label>
<?php echo form_input($username); ?></p>
<p><label for="password"><b>Lösenord:</b></label>
<?php echo form_input($password); ?></p>
<p><?php echo form_submit('login', 'Logga in'); ?></p>
<?php echo form_close(); ?>
When I include the login view file in the main page the variables $username and $password aren't valid.
main.php
Code:
$this->load->view('login');