Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get back to the redirecting page after login (codeigniter)?

I have login controller in my CI app:

function index()
{
    if($this->session->userdata('logged_in')==TRUE) 
        redirect('/success');

    $data['error']=$this->session->flashdata('errormessage');
    $this->load->view('auth',$data);
}

function process_login()
{

    $username=$this->input->post('username');
    $password=$this->input->post('password');

    if($password == "good_pwd")
    {
    $data=array('username'=>$username,'logged_in'=>TRUE);
    $this->session->set_userdata($data);
    redirect('/success');   
    }   
    else 
    {
        $this->session->set_flashdata('errormessage','Login failed');
        redirect('/failed');
    }
}

Thats my securing constructor in main controller:

function __construct()
{
    parent::__construct();
    if($this->session->userdata('logged_in')!=TRUE)  redirect('/login');
}

When I'm trying to get into www.mysite.com/main/function1/ and I'm not logged in, then constructor redirects me into login page - when I log in correct I'm being redirected into main home page instead of page which redirected me into login page (in this example case: www.mysite.com/main/function1/ ) - how to do it?

like image 657
pawel Avatar asked Aug 30 '12 09:08

pawel


People also ask

How to redirect user to another page in CodeIgniter?

While building web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect () function is used for this purpose. The first argument can have two types of URI.

Why is my CodeIgniter session being destroyed automatically?

Its not any core issue of codeigniter or something that is session destroyed automatically or something else.. It is actually the load of data amount in session, if we save the more & more data inside session with Codeigniter ("I am not sure about native session but for codeigniter I can confirm this"), it will destroy the session automatically.

Why myfriends redirects me to the login page?

In this scenario, when the user directly clicks on MyFriends, the system will redirect you to the login page, because the system requires a logged in user. After successful login, the system should open MyFriends page automatically. Check if the user is logged in or not in FriendList.aspx page.

How to send specific HTTP response code with redirects in PHP?

The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code. Create a controller called Redirect_controller.php and save it in application/controller/Redirect_controller.php


1 Answers

you'd need to store your request URI in a session for this, so you can return to the previous page, something along the lines of:

function __construct()
{
    parent::__construct();
    if($this->session->userdata('logged_in')!=TRUE) {
        $this->load->helper('url');
        $this->session->set_userdata('last_page', current_url());
        redirect('/login');
    }
}

... you can then use session data to redirect back

like image 144
Zathrus Writer Avatar answered Nov 08 '22 21:11

Zathrus Writer