Welcome Guest, Not a member yet? Register   Sign In
Rerouting a function or changing its url
#1

[eluser]Zac G.[/eluser]
Hi,

I have a function that logouts out a user. I would like it to send the user back to the homepage after they logout. How would I do this? Or can I change the url from site/logOut to just site after the logOut function runs?

I think this should be a pretty simple process, but I am a new to working with CI and not sure how to do this.

Thanks!
#2

[eluser]coolfactor[/eluser]
Use the redirect() function, as explained on this User Guide page.
#3

[eluser]Zac G.[/eluser]
That is what I was thinking...

I have this in my code:

Code:
$this->session->sess_destroy();
echo 'You are logged out';
$logged_in = FALSE;
if ($logged_in == FALSE)
{
redirect('/registration', 'refresh');
}

For some reason though, the echo doesn't display and the redirect doesn't go back to the index page.

It logs the person out, but when I press the back button the it still displays the user information.
#4

[eluser]chrisrivera[/eluser]
This is a late reply to your thread but looking at your code, you have a section in there that's a little useless:

Code:
$logged_in = FALSE;
if ($logged_in == FALSE)

You're setting the variable then running a conditional test on it, knowing that it will be false. What you would probably want to do is something like this. Another issue that I see is that you are echoing out content, then having the system redirect the user to the registration page. The redirect function is telling the browser, "go to this page: /registration", so the echo you are doing never shows. What you would probably want to do is something like this:

Code:
$this->session->sess_destroy();
redirect('/registration/loggedout', 'refresh');

You will need to make a modification to your code to support the 2nd segment in the uri to understand that when it sees loggedout, it should display your message of "You have been logged out".
#5

[eluser]Zac G.[/eluser]
@chrisrivera Thanks! The project had been put on hold for a while and won't be starting up again for a few weeks, so this was actually really great timing Smile
#6

[eluser]chrisrivera[/eluser]
Glad to hear that I could help.

Something else you may want to consider if doing a more internal redirect without calling redirect.

An example would be, if you have the Registration controller called, you can have a method called logout() that would do the logging out, then you could call internally a function that would show your login page, and pass a parameter either in the method call directly or by passing the value in a variable within the class, so that when you call the method that display the login page, the value of the message would be passed along.

Here is a hacked up code sample based on a controller class I use for my code where I do something similar.

Code:
class Registration extends Controller
{
  var $message = '';

  ...your existing code...

  function logout()
  {
    ... do the logout functionality here ...

    $this->message = 'You have been logged out.';
    $this->showLogin();
  }

  function showLogin()
  {
    $this->load->view('login', $this->message);
  }
}

This way you're passing the value internally and you don't have to worry about the message getting passed along in the url. I'm curious to hear how others feel about this technique when it comes to dealing with messages like this within the controller.




Theme © iAndrew 2016 - Forum software by © MyBB