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?
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With