8

I have a like button on my website (which is for users to like the fb fan page).

If the user clicks like, I check to see if the even has been fired (event subscribe), and then display some content to them.

What I would really appreciate help with is the following:

If the user is logged in to facebook AND they already like the page, I want it to display "You're already a fan!" and show the content. (rather than displaying the greyed out like button)

OR

If the user is not logged in to facebook and they click the like button, I want it to display "You're already a fan!" and show the content. (rather than displaying the greyed out like button)

edit: Guys, I've researched this on SO and have found similar questions but not quite what I'm after. Maybe I'm mistaken but if someone can provide a link to one which describes my exact problem, it would be a lot more helpful than a - on the question. I have checked the following:
Check if the user is connected to facebook and then check if he liked a page
Facebook Like Box: How to detect if user already liked the page?
Facebook LIKE button hiding when page is already LIKED by user
How to check whether user has liked the page or not using php/javascript
Check if user already likes fanpage

Community
  • 1
  • 1
Gazzzzza
  • 289
  • 2
  • 4
  • 9
  • I'm not one of the downvotes, but people might be more inclined to help if you include that comment in your question; link to some questions you've already tried and say why they don't help, so people know you've tried to do it yourself already. – Matt Sep 13 '11 at 06:48
  • possible duplicate of [How to check if a user likes my Facebook Page or URL using Facebook's API](http://facebook.stackoverflow.com/questions/5093398/how-to-check-if-a-user-likes-my-facebook-page-or-url-using-facebooks-api) – Igy Feb 19 '13 at 23:15

2 Answers2

3

If you use the facebook php API. i've came up with this short function that u can include inside the base_facebook.php file into the class BaseFacebook.

public function userIsFan() {
    $sr = $this->getSignedRequest();
    if($sr && is_array($sr)){
        if(array_key_exists('page', $sr)){
            return $sr['page']['liked'] == 1;
        }
    }
    return 0;
}
Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
gabo bernal
  • 721
  • 6
  • 5
  • 4
    Probably better to extend the BaseFacebook class instead of modifying it, otherwise the next time you come to use this function in a different project you'll wonder why it suddenly doesn't work when you grab a fresh copy of the base class. Alternatively if FB change their base class and you upgrade, your custom functions will all get lost (trust me, it happens...) – nealio82 Dec 20 '12 at 14:47
  • This is not working anymore. Signed request no more has information about page liking. – lukyer Jul 29 '15 at 07:35
0

Make sure you are using xfbml, this one works for me

FB.Event.subscribe('auth.authResponseChange', function(response) {
        FB.api('/me/likes/[page_id]',
          function(response) {
              //console.log(response); - see what the response is on your console.
              if(response.data[0])
                //use script to display your content alert("Liked");
              else
                // just do nothing, display like button alert("Not yet liked");
          }
        );

    });

page_id will be the id not the name of your page, to find out the id, just visit http://graph.facebook.com/[your fb page name]

hope this helps someone in the future.

RicardO
  • 1,219
  • 1
  • 14
  • 24