0

I have this code to log people in with Facebook:

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=my_app_id&amp;xfbml=1">
</script><fb:login-button show-faces="false" perms="user_hometown,user_about_me,email,user_address" autologoutlink="true" width="200" max-rows="1">
</fb:login-button>

<?php

if($facebook->getSession())
{
 // Nothing here yet
}

?>

But the call to $facebook->getSession() seems to break the page. Nothing below this call gets displayed. Any idea why? Or what I am doing wrong?

Here is the page where I am trying to test this: http://www.comehike.com/test_fb_connect.php

Genadinik
  • 18,153
  • 63
  • 185
  • 284

1 Answers1

3

If that's your entire PHP file then your referencing $facebook which hasn't been declared yet. You'll want to include the location of the PHP SDK then initialize $facebook with your appID and secret. This format has changed with v3.0.0 of the PHP SDK found here.

V 2.2.X

require '../src/facebook.php';

$facebook = new Facebook(array(
      'appId'  => '12345678910',
      'secret' => '*****************************',
    ));

$session = $facebook->getSession();
if ($session) {
  // proceed knowing you have a valid user session
} else {
  // proceed knowing you require user login and/or authentication
}

V 3.0.0

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '12345678910',
  'secret' => '*****************************',
));

$user = $facebook->getUser();
if ($user) {
  // proceed knowing you have a logged in user who's authenticated
} else {
  // proceed knowing you require user login and/or authentication
}
DSchultz
  • 1,335
  • 6
  • 15
  • Thanks - do I have to actually download the Facebook.php file and place it in my direcctory structure to make it work? – Genadinik Jul 29 '11 at 17:35
  • Yup! Since it's all server-side you do. What exactly are you trying to accomplish? The JS SDK may be a bit easier and still serve your use case. – DSchultz Jul 29 '11 at 17:36
  • I am trying to get some user credentials once the user logs in. How would I do this in JS? You are right -- JS would be way easier! – Genadinik Jul 29 '11 at 17:39
  • When you say user credentials, do you mean their facebook username and password or are you having them create new credentials for YOUR site? – DSchultz Jul 29 '11 at 18:09
  • I mean I want to pull in their facebook data. Then I'll use those credentials on my site to keep a record of what they did. So I need to be able to get their FB name, lat,lng, email...and insert it into my db. – Genadinik Jul 29 '11 at 18:14
  • I would use the JS SDK then. You can use the FB.login() to log the user in. Then use FB.api to query '/me' which will give you the basic info of the currently logged in user, all via the JS SDK. – DSchultz Jul 29 '11 at 19:28