Start learning with our library of video tutorials taught by experts. Get started

Building Facebook Applications with PHP and MySQL

Logging out users from your app


From:

Building Facebook Applications with PHP and MySQL

with Ray Villalobos

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Building Facebook Applications with PHP and MySQL
1h 45m Intermediate Jan 16, 2013

Viewers: in countries Watching now:

Learn the basics of building complex, data-driven applications with the Facebook PHP SDK and MySQL. Author Ray Villalobos first introduces the fundamentals, such as checking to see if a user is logged into an application, accessing Facebook user data through the Open Graph API, and making complex API calls with the Graph API Explorer. The course then dives into building an application with API paths and Facebook Query Language (FQL) calls. Along the way, you'll discover how to post to a user's wall, upload photos, integrate with webpages, and more.

Topics include:
  • Downloading and installing the PHP SDK
  • Logging in and authenticating users
  • Accessing Open Graph data
  • Working with connection subpaths, limits, and subsearches
  • Styling a Facebook app with CSS
  • Setting up pagination
  • Understanding Facebook Query Language (FQL)
  • Integrating query results into a full-scale application
Subjects:
Developer Web Databases Web Development
Software:
Facebook MySQL PHP
Author:
Ray Villalobos

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.

There are currently no FAQs about Building Facebook Applications with PHP and MySQL.

Share a link to this course
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.
Upgrade now


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.

join now

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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