Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Logging out users from your app

From: Building Facebook Applications with PHP and MySQL

Video: Logging out users from your app

Once users are logged in and authenticated into your application, you can start accessing Facebook's User Data, called the Open Graph, because our application will be changing often, I am going to add a way for users to log out of our application. You don't always have to do this, but it might be useful during development. So let's take a look. So in the last video we saw that there is a method of the Facebook object called getLoginUrl, so naturally you're probably thinking there's an equivalent getLogoutUrl, and you'd be right. Let's take a look at the documentation for that method, you can see that it's pretty much just the same as the getLoginUrl method, but it only takes in one parameter an optional URL to redirect the user after they logged out. So let's try adding that into our project.

Logging out users from your app

Once users are logged in and authenticated into your application, you can start accessing Facebook's User Data, called the Open Graph, because our application will be changing often, I am going to add a way for users to log out of our application. You don't always have to do this, but it might be useful during development. So let's take a look. So in the last video we saw that there is a method of the Facebook object called getLoginUrl, so naturally you're probably thinking there's an equivalent getLogoutUrl, and you'd be right. Let's take a look at the documentation for that method, you can see that it's pretty much just the same as the getLoginUrl method, but it only takes in one parameter an optional URL to redirect the user after they logged out. So let's try adding that into our project.

Make sure we check for the user haven't been logged in, and so what I'll do is I'll create a logoutUrl variable here, and point it to the Facebook object and use the getLogoutUrl method. Then I'll just print out the link to logout. So let's go ahead and save this, and we'll switch back to our application, we'll refresh, and if you have been playing around with this application, you'll probably see the User ID showing up for your user with the logout link.

If not, just go ahead and log in and then you should see something like this. So I'm going to click on the logout link, and we'll see what happens. Nothing looked like it happened, but if you refresh the page, you'll see that you actually get locked out of Facebook, which is sort of what you'd expect to happen. But if you log in again, you'll be logged back into your app, and that is sort of okay. But what I really want to happen is to be able to logout of the application without logging out of Facebook so that I can re-authenticate the user with maybe different permissions.

So let's see how we would do that. I've actually created a separate link called the logout.php, so let me open that up and show you what that code looks like. So if you take a look at the very top of this code, it's almost exactly as the top of our index.php code right here. It loads up the facebook.php library and then creates a Facebook object. After that we're clearing a cookie by setting the timer to -100 ms at our website URL then we destroy this session, that gets rid of the temporary session that Facebook will create when you log in to an application, and then we set the location back to the original document, which is this page here.

So really all we have to do now is just modify this logOutUrl link so it just points out directly to that document. So I'm not going to use this logOutUrl variable, I'll just get rid of it. So now I'm going to Save and switch back to my application and then I'll refresh and then I'll hit the logout button. So you could see that I'm still logged into Facebook but I'm not logged into my application. So if I want to log back into my application, I can hit login, and now I can see the User ID just fine and logout when I want to.

Now that's going to be useful if you want to change the scope of what you have access to. So if you come right here, and you add something like scope and then we type in some permissions that we want here, say, for example, email, and I'm going to save this document and come back here and then I'm going to refresh this page, logout and log back in. And you'll see that it's asking me for additional permissions for this application. I've just logged out of Facebook, and I logged back in, I might not see this, so I'm going to hit Allow, and now my application is going to have the right permissions for accessing not only the basic info, but also email, and I didn't have to log out and log back into Facebook in order to do that.

So logging out users should be pretty straightforward, but you might run into some problems if you just use the getLogoutUrl method. For application, it's just easier to create a separate logout file. Remember that in production, we're not always going to need to give users the ability to logout. So you might want to take the ability to log out, out of your application before you launch it.

Show transcript

This video is part of

Image for Building Facebook Applications with PHP and MySQL
Building Facebook Applications with PHP and MySQL

24 video lessons · 8421 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Building Facebook Applications with PHP and MySQL.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.