Logging Out4:00 with Alena Holligan
The final part of logging in, is a logout system. Let's reverse the logic for log in.
$session->remove('auth_logged_in'); $session->remove('auth_user_id'); $session->remove('auth_roles');
The final part of a log in system is logging out. 0:00 Let's create the new procedure. 0:04 We'll name this doLogout.php. 0:11 And once again, we'll need the bootstrap file. 0:18 Let's copy from here. 0:22 Okay, to log out a user we need to remove all 0:28 the authenticated sessions that we set on log in. 0:32 So let's copy those from our doLogin. 0:36 Actually, let's copy all those lines, Great, 0:45 now, instead of set, we want to say remove. 0:49 And then we don't need to pass any value. 0:59 Great, now we know that all of our sessions are removed. 1:08 Next, we can change our flash message. 1:14 Success, successfully logged out, and 1:17 redirect to the login page. 1:22 Let's test this out in a browser. 1:29 We'll choose Log Out, and we get successfully logged out. 1:33 Then log in, and we're good. 1:38 We can log out and we can log back in. 1:45 Now we're ready to automatically log in a newly registered user. 1:48 Let's go take a look at our doLogin procedure. 1:53 To log in on registration we'll need to save these same session variables 2:01 that we did on login. 2:06 So let's move these session variables into a function in our functions auth file. 2:07 We'll cut from here, and then our functions auth, we'll add a new function. 2:13 And we'll call this saveUserSession. 2:23 And we'll need to accept the user. 2:29 And we'll paste our code. 2:36 Remember, we need to add our global 2:41 session since we're using this inside of function. 2:44 I don't want my redirect here, instead I´ll save this, and 2:49 I'll go back to my doLogin, put the redirect here. 2:54 And call my saveUserSession, 2:57 and I'll pass my user. 3:02 We can now use this saveUserSession in our do register procedure as well. 3:08 Undo register After our user, 3:16 we're going to save user session, and we'll pass our user. 3:21 Well, let's go to the browser and try registering another user. 3:27 We can log out and then register, and this time we'll register user. 3:33 And this time we'll register our user. 3:37 Great, we now see that we've successfully logged in, and the user has been added. 3:43 We can now log out, log back in, and register. 3:48 We're ready to lock down specific functionality to only logged in users. 3:53
You need to sign up for Treehouse in order to download course files.Sign up