Codeigniter Redirect to desired url after user login

Viewed 382

I have a website with Codeigniter 3 framework.

There is a login page as www.example.com/login

If author login to the panel they are redirecting to main page www.example.com/author/feed.

There are many pages in Author controller such as my-profile, edit-profile, reset-password etc.

If author wants to access www.example.com/author/my-profile they are redirected to www.example.com/login, after login they are redirected to to main page www.example.com/author/feed.

But I want them to redirect to www.example.com/author/my-profile.

What function I need to use in my code??

2 Answers

You can use the helper function redirect(). You can learn more here.

// after login
redirect('/author/my-profile');

You need to save your desired route in session before redirecting to login page like this:

$this->session->set_userdata('page', base_url('my-profile'));

When a user logs in, you can check if page is stored in session, otherwise redirect to base url:

$pageLink = base_url();               
if ($this->session->userdata('page')) {
    $pageLink = $this->session->userdata('page');
    $this->session->unset_userdata('page');
}

redirect($pageLink);
Related