CodeIgniter Forums
how to back to the lastpage that we opened after login - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to back to the lastpage that we opened after login (/showthread.php?tid=18493)

Pages: 1 2


how to back to the lastpage that we opened after login - El Forum - 05-08-2009

[eluser]eoinmcg[/eluser]
[quote author="Dam1an" date="1241798607"]

I've had bad experiences with that, can't remember what I was trying to do, but it either wasn;t always set, or wasn;t always accurate :-S[/quote]

Yeah, if a user fails the login the first time the value of HTTP_REFERER is obviously going to change. So, what I would do then is if the referrer has been POSTED it's populated back into the form. Generally this solution has worked well for me in the past but I'll definitely have a wee look at using flash data.


how to back to the lastpage that we opened after login - El Forum - 05-08-2009

[eluser]drewbee[/eluser]
As a note, make sure you save the flash data only on the initial display of the login form. If the user enters invalid credentials, you want to make sure 1. You don't overwrite where they originally came from and 2. if you are using flashdata, make sure to call keep_flashdata() to keep it from removing itself.


how to back to the lastpage that we opened after login - El Forum - 05-08-2009

[eluser]yudahebat[/eluser]
can you give me the example of this function??
help me please, I need for my college project..
thxz before..


how to back to the lastpage that we opened after login - El Forum - 05-08-2009

[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 *