[eluser]Santiago DimattÃa[/eluser]
- Flashdata will be resetted if you open another page AFTER the redirect to the login.
- Userdata will be rewrite if you open another protected page.
----
To do this I use something like this:
Protected page
Code:
<?php
class Test extends Controller {
function Test()
{
parent::Controller();
//$this->load->library('session');
}
function index()
{
if($this->auth->is_logged_in()){
$data['text'] = 'welcome!';
$this->load->view('test', $data);
}
else
{
//$this->session->set_flashdata('referer', $this->uri->uri_string());
$data['url_to_redirect'] = $this->uri->uri_string();
$this->load->view('login', $data);
}
}
}
The login view:
Code:
<html>
<head>
<title>Test page</title>
</head>
<body>
<form method="post" action="/login/">
<input type="hidden" value="<?php echo $url_to_redirect; ?>" name="url" id="url" />
<input type="text" value="Your username" name="username" id="username" /><br />
<input type="text" value="Your password" name="password" id="password" /><br />
<input type="submit" value="Login" />
</form>
</body>
</html>
And login controller:
Code:
<?php
class Login extends Controller {
function Login()
{
parent::Controller();
}
function index()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$url_to_redirect = $this->input->post('url');
if($this->auth->login($username, $password)){
redirect($url_to_redirect);
}
else
{
$this->load->view('loginfail');
}
}
}
* Code not tested *