Welcome Guest, Not a member yet? Register   Sign In
Auth system knowing where you come from
#1

[eluser]Kurai[/eluser]
Hi there!
In the app I building I have an authorization system which I built from scratch.
Basically, when I come to a page which needs login, it checks if there's an open session. If so, it just send you to the auth system for the login.

Now, here's the problem (maybe stupid, but I'm a bit of a newbie here): at first, I've got only a landing page. Now, I would like the system to remember the exact page which sent the user to the auth routine, so it can send him back exactly to the page where he came from.

How can I do it?

Thank you!
#2

[eluser]wiredesignz[/eluser]
routes.php
Code:
//login/:any routed to login->index($any)
$route['login/(.*)'] = 'login/index/$1';

auth check library
Code:
//auth check failed so send user to login controller with this uri attached
redirect('login'.$this->uri->uri_string());

login controller
Code:
//get the return path from the uri
$path = implode('/', array_slice($this->uri->rsegments, 2));

//login succeeded so send back
redirect($path);
#3

[eluser]Kurai[/eluser]
It doesn't work. there must be some kind of error when I redirect, because the uri string is obviously correct, but it keeps me bouncing back to the home page.
From what I see, it seems not to recognize the $redir in the redirect function. If I do something like: redirect ($redir . "something"); it sees just the "something part. Strange.

Code:
function login()
    {

        //setto le regole di validazione dei campi del form
        $rules['username']    = "trim|required|xss_clean|callback_check_logged|callback_check_password";
        $rules['password']    = "trim|required|strtolower|md5";

        $this->validation->set_rules($rules);
        
        //setto i campi che vengono ricompilati automaticamente in caso di errore
        $fields['username'] = 'Username';
        
        $this->validation->set_fields($fields);
        
        //valido l'input e metto il risultato in una variabile
        $validation = $this->validation->run();
        
        //se l'input non รจ valido ritorno al form, altrimenti pagina di successo. In caso di errore db, ancora al form.
        if ($validation == FALSE)
        {
            $this->load->view('loginview');
        }
        else
        {
            $name=$this->validation->username;
            $pass=$this->validation->password;
            $this->user->login($name, $pass);
            $redir=implode('/', array_slice($this->uri->segments, 2));
                redirect($redir, 'location');
        }
    }
#4

[eluser]adamp1[/eluser]
This is rather simple to do. All you need to use is flashSessions. When you check a user has permission to access a page, if they don't you store the $this->uri->uri_string() into a flashSession variable. Then when you show the login form you must stop it getting deleted (since it only lasts 1 page load). Then when you verify there login details you send them to the value stored in the flash variable. So something along the lines of:

Check the user has access:
Code:
function check()
{
  if ( $this->CI->session ) {
    // CHECK ACCESS PERMISSIONS            
  }
            
  // DENIED ACCESS  
  $this->CI->session->set_flashdata('requested_page',$this->CI->uri->uri_string());
  redirect('auth/login','location');
}

Display Login Form:
Code:
function login_form()
{
  if ( $this->CI->validation->run() === FALSE ) {
    // DISPLAY FORM
    $this->CI->session->keep_flashdata('requested_page');
  }
  else {
  // LOG USER IN
  $this->_login();
  }
}

Log user in function:
Code:
function _login()
{
  // CHECK THE LOGIN DETAILS ARE VALID
  // IF NOT REDIRECT BACK TO FORM

  // OTHERWISE LOG THE USER IN AND
  // Redirect to requested page
  if(FALSE !== ($page = $this->CI->session->flashdata('requested_page')))
    redirect($page);

  // REDIRECT NORMALY
  redirect('admin');            
}

There you go, that's how I do it.
#5

[eluser]wiredesignz[/eluser]
This is wrong also:
Code:
//not segments
$redir=implode('/', array_slice($this->uri->segments, 2));

//should be rsegments
$redir=implode('/', array_slice($this->uri->rsegments, 2));
#6

[eluser]erik.brannstrom[/eluser]
I have found this thread immensely useful: [library] History. Check it out!




Theme © iAndrew 2016 - Forum software by © MyBB