Check if user is logged in New Facebook API

well I'm new to the facebook api stuff, and just recently someone told me I was using the old api.

So now, I can't seem to figure out how to check if the user is logged into my site with the new api.

all I really have is

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({appId: '(appid)', status: true, cookie: true, xfbml: true});
  FB.Event.subscribe('auth.sessionChange', function(response) {
    if (response.session) {
      // A user has logged in, and a new cookie has been saved
    } else {
      // The user has logged out, and the cookie has been cleared
    }
  });
</script>

and a login button.

If anyone could show me how to check if someones logged in, (or is that it right there?) because I'd like it to change the login button located in a div called

<div id="fbuser">

to welcome the user.

Or even if you can point me towards some useful tutorials, that would be great!

Thanks

Answers


Here is an example where fbLoginStatus() function will be automatically called when user logs in/logs out, so you can decide which div to display there.

<body>
<div id="fb-root"></div>
<script>
  function fbLoginStatus(response) {
     if( response.status === 'connected' ) {
        //user is logged in, display profile div
     } else {
        //user is not logged in, display guest div
     }
  }
  window.fbAsyncInit = function() {
    FB.init({appId: 'your app id', status: true, cookie: true, xfbml: true});
    FB.getLoginStatus(fbLoginStatus);
    FB.Event.subscribe('auth.statusChange', fbLoginStatus);
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

Now if you need to check if user is logged in right now (for example on button click):

if(FB.getSession() != null) {
   //logged user id: FB.getSession().uid
} else {
  //user is not logged in
}

Sorry, don't know any good tutorials.


Need Your Help

Cannot add foreign key constraint - MySQL ERROR 1215 (HY000)

mysql foreign-keys

I am trying to create database for gym management system, but I can't figure out why I am getting this error. I've tried to search for the answer here, but I couldn't find it.

.htaccess deny access to specific files? more than one

php security apache .htaccess file

I am able to disable access to a file with .htaccess, but I don't know how to disallow multiple files to be viewed (directly, not from includes)