10

Well it's not really a problem but I check if the user exist and log them in and redirect to site/members_area, but I don't want to send the user to a specific page but i want to reload the current controller. So if I login in index/home I would like to be redirected at index/home, how should I proceed?

in regular php I would put in the action to redirect to current page

<?php echo $_SERVER['PHP_SELF']; ?>

This is the code in the framework

function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site/members_area'); //<-- this line here should be dynamic
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }

3 Answers 3

15

I solved this problem myself by having a login form in the header that always submits to one login controller, but the catch is that the header login form (which appears on every page) always has a hidden input called redirect which the actual login controller captures...

Here's the basic set up (make sure the url helper is loaded):

The Header Login Form

<form action="/login" method="post">
    <input type="hidden" name="redirect" value="<?php echo current_url(); ?>" />
    <input type="text" name="username" value=""  />
    <input type="password" name="password" value=""  />
    <input type="submit" name="login" value="Login" id="submit">
</form>

The Login Controller Form

<form id="login" action="" method="post">
    <input type="text" name="username" id="username" value="" />
    <input type="password" name="password" id="password" value=""/>

    <?php if(isset($_POST['redirect'])) : ?>
    <input type="hidden" name="redirect" value="<?php echo $_POST['redirect']; ?>" />
    <?php endif; ?>

    <input type="submit" name="login" id="submit" value="Login" />  
</form>

The best part is you keep setting the redirect on failure and the redirect input only gets set if you're logging in from somewhere else.

The Controller

function index()
{
    if( ! $this->form_validation->run())
    {
        // do your error handling thing
    }
    else
    {
        // log the user in, then redirect accordingly
        $this->_redirect();
    }   
}

function _redirect()
{
    // Is there a redirect to handle?
    if( ! isset($_POST['redirect']))
    {
        redirect("site/members_area", "location");
        return;
    }

    // Basic check to make sure we aren't redirecting to the login page
    // current_url would be your login controller
    if($_POST['redirect'] === current_url())
    {
        redirect("site/members_area", "location");
        return;
    }

    redirect($_POST['redirect'], "location");
}

What's happening here is this:

  1. User logins on a different page.
  2. The login form submits to a single login controller with a hidden input element stating where they are logging in from.
  3. The login controller processes the login, then redirects based on the input.
  4. On failed login the redirect keeps getting set again, so no matter what, the user will return to the original page.

That's just a basic example. You can obviously tweak it as needed.

1
  • "by having a login form in the header" .. I'm sorry but I'm lost at there. What you mean ?
    – Hendry H.
    May 9, 2013 at 14:29
11

You can do it like this. Remember to load the session library and url helper.

$this->session->set_flashdata('redirectToCurrent', current_url());

Pass the above flashdata along with the login and redirect using:

redirect($this->session->flashdata('redirectToCurrent'));
1
  • 1
    the session library and url heper were loaded. But I'm not sure on where to put the code, the redirect is of course added in the login controller which logins the user but i'm not sure where to put the $this->session->set_flashdata('redirectToCurrent', current_url()); I added both lines in the same function but it redirects me back to the main controller (index)
    – Christophe
    Apr 17, 2010 at 8:00
2

I'm sure there may be a better way, but the way I do it is when the check if the user is logged in fails I use $this->session->set_flashdata('redirect_url', current_url()); and then pass it along with the login form so I know where to redirect the user back to.

Like I say, I'm sure there's a cleaner way to do this but I definitely don't like $_SERVER['HTTP_REFERER']; as it can't really be trusted.

3
  • You have to be sure that the URL helper is loaded. Sorry, I should have mentioned that. If that's not it, what kind of error are you getting?
    – vernonk
    Apr 16, 2010 at 19:18
  • Sorry... just looked again... It would be... redirect($this->session->flashdata('redirect_url'));
    – vernonk
    Apr 16, 2010 at 19:18
  • 1
    sorry for late response but I'm just being forward to the homepage again
    – Christophe
    Apr 27, 2010 at 11:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.