Home arrow PHP arrow Page 3 - Facebook PHP API Applications: A Second Look

Is the user logged in? - PHP

This is the second part of the Facebook PHP API tutorial. The first part ran on August 11. It covered the most basic features, such as adding the basic Facebook “Like” button, the “Recommendations” plugin and registering your applications in Facebook to get an Application ID and Secret key. In this part, we'll delve deeper.

  1. Facebook PHP API Applications: A Second Look
  2. Understanding the Facebook Graph API
  3. Is the user logged in?
  4. Final Facebook PHP API Script - Display basic user information
By: Codex-M
Rating: starstarstarstarstar / 5
August 16, 2010

print this article



Now that you have a fully working PHP function to get cookies, let's find out whether or not the user is logged in. If the user is not logged in, the code should present the Facebook login button; otherwise, it should retrieve user data from Facebook social graph (https://graph.facebook.com/) for the corresponding user ID.

<?php if ($cookie) {
//cookie is set, user is logged in
$user = json_decode(file_get_contents('https://graph.facebook.com/'.$cookie['uid']));
//Display the facebook user ID, name, gender and Facebook URL in the web browser
echo '<br />';
echo 'Your Facebook ID: '.$user->{'id'};
echo '<br />';
echo 'Your name: '.$user->{'name'};
echo '<br />';
echo 'Your gender: '.$user->{'gender'};
echo '<br />';
echo 'Your Facebook URL: '.$user->{'link'};
echo '<br />';
echo '<fb:login-button autologoutlink="true"></fb:login-button>';
//user is not logged in, display the Facebook login button
echo '<h2>Facebook Application Test page</h2>';
echo '<br />';
echo 'This is the most basic Facebook application PHP source code that will grab the user Facebook full name, gender and Facebook URL.';
echo '<br />Then displays those information in the web browser once the user has successfully logged in';
echo '<br /><br />';
echo '<fb:login-button autologoutlink="true"></fb:login-button>';

Code Discussion

The PHP condition that checks to see if the cookie is set, is this: if ($cookie)

Basically the cookie value is defined by the previous get_facebook_cookie function: $cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

If the user is logged in, the profile information can be retrieved from Facebook using this function:

$user = json_decode(file_get_contents('https://graph.facebook.com/'.$cookie['uid']));

 Since the data are JSON objects, it is decoded using the php function json_decode in order for it to be properly displayed on the browser.

To get the profile information from the decoded JSON objects, set up your echo command this way:

echo 'Your Facebook ID: '.$user->{'id'};
echo '<br />';
echo 'Your name: '.$user->{'name'};
echo '<br />';
echo 'Your gender: '.$user->{'gender'};
echo '<br />';
echo 'Your Facebook URL: '.$user->{'link'};

$user->{'id'} contains the decoded user ID, while user->{'name'} contains the decoded Facebook user name, etc.

The Facebook login button is this:

 echo '<fb:login-button autologoutlink="true"></fb:login-button>';

This will display "Login" if the user is not yet logged in and "LoggedOut" if the user is logged in to Facebook.

This is the screen shot of the PHP application when the user is not logged in.

When the user clicks the login button, a pop-up window will then show, and the user will enter their email and password as the required information for logging in.

After logging in, once the application retrieves the user information from Facebook's social graph, it will look like the screen shot below:

The required JavaScript SDK at the bottom of Facebook PHP apps

Javascript SDK is a client side script responsible for providing functionality for authentication and sharing. You can read more information about the Facebook JavaScript SDK at the link. 

This code is required; it is placed at the bottom of your Facebook PHP web application:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: