Well this is a simple problem of 'redirecting back to original page'. You are thinking it through correctly but you fail by misunderstanding view
vs redirect()
.
Process:
User lands on url /view/article/what-is-the-best
. They are not logged in so they click the login button which shows the login form. The submit the form and are send to /account/login
after a successful login.
What to change in the process:
When the user logs in you need to do the following to capture their referrer
so load user_agent and capture the redirect into your session:
$this->load->library('user_agent');
$this->session->set_userdata('redirect_back', $this->agent->referrer());
So with that done, you have a session variable called redirect_back
that holds the original page URL.
After login, you do a simple redirect (NOT loading a view since it keeps the same controller route):
redirect( $this->session->userdata('redirect_back') );
You could add something to check you get a referred too (in case of some oddity where you don't catch a referring url).
That should do it for you tho, get a user back to the original spot prior to login.