Welcome Guest, Not a member yet? Register   Sign In
Redirect() and came from
#1

[eluser]Zeff[/eluser]
Hi folks,

I wrote a simple login library/controller that I can call when visitors want to get restricted content. I was wondering if there is a specific funtion in CI or 'best-practise' approach to see from which 'controller/method' a user called the login controller, to be able to redirect them from my login controller back to the requesting URI (controller/method)?

Many thanks in advance!

Cheers,

Zeff
#2

[eluser]Phil Sturgeon[/eluser]
Before you redirect set the URI in $this->session->userdata(), then call it back on successful login so you can redirect them back.
#3

[eluser]Bastian Heist[/eluser]
You can even use flashdata so you dont have to care for unsetting it afterwards.
#4

[eluser]Phil Sturgeon[/eluser]
The issue there is that you need to either keepalive the flashdata for each request to the login page or enter the redirect URI into a hidden input in the logic form to keep it.

Neither way is perfect. :down:
#5

[eluser]OES[/eluser]
Infact there is a real simple way to do this.

Here is a example bit of code within a check_access method in MY_Controller. This is within a check_access function and is only fired depending on where uri segment is for the user.

Code:
$id = $this->session->get($area_name . '_id');
$user_areas = $this->session->get('user_areas');

if(empty($id) OR !is_array($user_areas)):
    if(strpos($_SERVER['REQUEST_URI'], 'login/') === FALSE):
        $this->session->set_userdata('last_url', urlencode($_SERVER['REQUEST_URI']));
        redirect("login/$area_name", 'location', 302, true);
    endif;
endif;

So once the user has been redirected to the login area you will have there last url in the session.

Once they login just redirect and clear session

Hope this helps.

Lee
#6

[eluser]Zeff[/eluser]
Dear all,

I realized there is no other way than to store the 'came from' url in a session var Confusedmirk:
Thanks for your smart approach Lee ;-)

Thank you all for your suggestions!

Kind regards,

Zeff.




Theme © iAndrew 2016 - Forum software by © MyBB