CodeIgniter Forums
redirect after login - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: redirect after login (/showthread.php?tid=48176)

Pages: 1 2 3


redirect after login - El Forum - 01-06-2012

[eluser]UnknownPlayer[/eluser]
Hi,
can someone give me code how can i redirect after login on refferer page ?
I had php code, but it doesn't work always, so if anyone have good example ?


redirect after login - El Forum - 03-07-2012

[eluser]Kraig[/eluser]
[quote author="Ayeyermaw" date="1325927347"]I use tend to use
Code:
$this->router->fetch_class()
in MY_Controller to determine what controller the user is trying to get to. I then set that in a session variable, which the login controller can use to redirect the user back to the correct page after login.
I just woke up so wouldn't dare write example code for you incase it was wrong but hope that helps[/quote]


Are you meaning that with this you, after login the user will be redirected to their current page? I am having problems using $link = current_url(); because it is redirecting the user back to a function in the login class.


redirect after login - El Forum - 03-08-2012

[eluser]Ayeyermaw[/eluser]
[quote author="Kraig" date="1331182666"]
Are you meaning that with this you, after login the user will be redirected to their current page? I am having problems using $link = current_url(); because it is redirecting the user back to a function in the login class.[/quote]

Without knowing how you are using current_url() it's difficult to help you here so i'll give you and example using it.

In MY_Controller:
Code:
public function __construct()
{
    parent::__construct();

    $logged_in = /* Code to check if user is logged in here */ ;
    if (!logged_in)
    {
        $this->session->set_userdata('return_url', current_url());
        redirect('MyLoginController');
    }
}

and in your login controller (Note: MyLoginController should not extend MY_Controller) you'd have something like this:

Code:
// On successful login
$redirect_url = base_url().$this->session->userdata('return_url');
redirect (redirect_url);

The above code is untested but it should give you an idea of what to do.
Hope that helps











redirect after login - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
I have a home controller that has a validate_credentials() function. So when the user attempts to login this is the function that gets called no matter what page they are on. The problem I am having is that when I am on a different page and attempt to login, it redirects me back to the validate_credentials page when using current_url(). It still logs me in, but I want it to redirect back to the previous page.

By the way I am using a jQuery login...so I'm on the current page until it redirects me.


redirect after login - El Forum - 03-08-2012

[eluser]Ayeyermaw[/eluser]
You need to go through it step by step and figure out where current_url is being set to the validate_credentials link. I can't help you here as I can't see your code.

My guess is that your jquery login might be using ajax to check login credentials. If that's the case then current_url will be whatever url your ajax call is using. Not where your browser is pointing


redirect after login - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
Ok, my login form is this: I am using an Ajax login
Code:
<?php echo form_open('home/validate_credentials'); ?>
     <h1>Member Login</h1>
                    
                    &lt;?php
      
      if($this->session->userdata('msg', 'reg-msg'))
      {
       echo '<div class="err">'.$this->session->userdata('msg').'</div>';
      }else{}
     ?&gt;
    
     <label class="grey" for="username">Username:</label>
     &lt;input class="field" type="text" name="username" id="username"
      value="" size="23" /&gt;
     <label class="grey" for="password">Password:</label>
     &lt;input class="field" type="password" name="password" id="password"
      size="23" /&gt;
              <label>&lt;input name="rememberMe" id="rememberMe" type="checkbox" checked="checked" /&gt; &nbsp;Remember me</label>
           <div class="clear"></div>
     &lt;input type="submit" name="submit" value="Login" class="bt_login" /&gt;
    &lt;/form&gt;
Notice that the forms action is to validate_credentials()

My Home Controller function is this:
Code:
public function validate_credentials()
{
  // Checking whether the Login form has been submitted
  if($this->input->post('submit'))
  {
   // Will hold errors
   $err = array();
  
   $this->load->library('form_validation');
  
   // field name, error message, validation rules
   $this->form_validation->set_rules('password', 'Password', 'trim|required');
   $this->form_validation->set_rules('username', 'Username', 'trim|required');
  
   if($this->form_validation->run() == FALSE)
   {
    $err[] = 'All the fields must be filled in!';
   }
    
   $this->load->model('membership_model');
   $q = $this->membership_model->validate();
  
   if($q) // if the user's credentials validated...
   {
    //if(isset($this->input->post('rememberMe'))) // Is rememberMe checked?
    //{
    
    //}
    $data = array(
     'username' => $this->input->post('username'),
     'password' => $this->input->post('password'),
     'is_logged_in' => true
    
    );
    
    $this->session->set_userdata($data);
    redirect('home/home');
   }
   else
   {
    $this->index();
    $err[] = 'Username and or password are incorrect.';
   }
  
   if($err){
    $data = array(
     'msg' => implode('<br />',$err),
     'reg-msg' => ' '
     );
    $this->session->set_userdata($data);
    // Save the error messages in the session
   }
   redirect('home/home');
  }
}
You can see that I have redirects to home/home, but that's all I can do. If I put current_url() that will return http://localhost:8888/ci/home/validate_credentials as the url no matter where I login from. For instance, I can be at http://localhost:8888/ci/acp/home and as soon as I click login the current_url() will still return http://localhost:8888/ci/home/validate_credentials, because I am calling the function validate_credentials() that is in the Home Controller. Is there anything I can do to redirect back to the page where I logged in at?


redirect after login - El Forum - 03-08-2012

[eluser]Ayeyermaw[/eluser]
current_url() will always be the value of the url you have requested so that's why after you post you are getting that value.
You have to save the value of current_url() to a session variable at the page where you want to return to. You can do that using my example above or you can set it in the construct of every individual controller just before you redirect to the login page.
From that point on you'll have the redirect page in session memory and you can call on it in your validate_credentials function.

instead of
Code:
redirect('home/home');
you'd have something like

Code:
redirect($this->session->userdata('return_url'))



redirect after login - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
So in order for this to work I would need to add
Code:
if (!logged_in)
    {
        $this->session->set_userdata('return_url', current_url());
    }

to every Controller construct?? Doesn't that seem a little redundant, especially if you are going to use it for every page?


redirect after login - El Forum - 03-08-2012

[eluser]Ayeyermaw[/eluser]
Nope... scroll up and read the part about MY_Controller


redirect after login - El Forum - 03-08-2012

[eluser]Kraig[/eluser]
Isn't MY_Controller just the name of the controller that you are in? So if I have 3 controllers leading to 3 different pages I would have to include

Code:
public function __construct()
{
     parent::__construct();

     $is_logged_in = $this->session->userdata('is_logged_in');
     if (!$is_logged_in)
     {
         $this->session->set_userdata('return_url', current_url());
     }
}

into every controller file. I notice that you said you use
Code:
$this->router->fetch_class()
but then you would have to add that to every MY_Controller file. Maybe I am thinking about this all wrong...