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) { 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'}; $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>
blog comments powered by Disqus |
|
|
|
|
|
|
|