How to check IF user has ALREADY liked the facebook page?

Asked
Active3 hr before
Viewed126 times

6 Answers

check
90%

I am trying to create a Facebook iFrame app. The app should first show an image and if the user likes the page, he will get access to some content.,And, if the user has liked the page:,You need to write a little PHP code. When user first click tab you can check is he like the page or not. Below is the sample code, @dwarfy No the don't login to my site. How is it that the Facebook like box know if you like a page or not? Is it just because it's facebook? – kel Jun 17 '11 at 3:11

The content of the BODY of the page :

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : 'YOUR APP ID',
    status : true, 
    cookie : true, 
    xfbml  : true  
  });
</script>

<div id="container_notlike">
YOU DONT LIKE
</div>

<div id="container_like">
YOU LIKE
</div>

The CSS :

body {
   width: 520 px;
   margin: 0;padding: 0;border: 0;
   font - family: verdana;
   background: url(repeat.png) repeat;
   margin - bottom: 10 px;
}
p, h1 {
   width: 450 px;margin - left: 50 px;color: #FFF;
}
p {
   font - size: 11 px;
}

#container_notlike, #container_like {
   display: none
}

And finally the javascript :

$(document).ready(function() {

   FB.login(function(response) {
      if (response.session) {

         var user_id = response.session.uid;
         var page_id = "40796308305"; //coca cola
         var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + user_id;
         var the_query = FB.Data.query(fql_query);

         the_query.wait(function(rows) {

            if (rows.length == 1 && rows[0].uid == user_id) {
               $("#container_like").show();

               //here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page.

            } else {
               $("#container_notlike").show();
               //and here you could get the content for a non liker in ajax...
            }
         });

      } else {
         // user is not logged in
      }
   });

});

In your action controller :

decoded_request = Canvas.parse_signed_request(params[: signed_request])

And then its a matter of checking the decoded request and display one page or another .. (Not sure about this one, I'm not comfortable with ruby)

decoded_request['page']['liked']

And here is the related Canvas Class (from fbgraph ruby library) :

 class Canvas

 class << self
 def parse_signed_request(secret_id, request)
 encoded_sig, payload = request.split('.', 2)
 sig = ""
 urldecode64(encoded_sig).each_byte {
    | b |
       sig << "%02x" % b
 }
 data = JSON.parse(urldecode64(payload))
 if data['algorithm'].to_s.upcase != 'HMAC-SHA256'
 raise "Bad signature algorithm: %s" % data['algorithm']
 end
 expected_sig = OpenSSL::HMAC.hexdigest('sha256', secret_id, payload)
 if expected_sig != sig
 raise "Bad signature"
 end
 data
 end

 private

 def urldecode64(str)
 encoded_str = str.gsub('-', '+').gsub('_', '/')
 encoded_str += '='
 while !(encoded_str.size % 4).zero ?
    Base64.decode64(encoded_str)
 end
 end

 end
load more v
88%

Hello Reader's, If you are making the code for facebook page likes details and you want to know if the user has already liked your page or not, Then you can use the following library code for that :-

An now open the channel.html file and just write the following code:-

<script src="//connect.facebook.net/en_US/all.js"></script>
72%

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.,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) ,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)

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;
}
load more v
65%

Permissions Reference,The Facebook Pages that this person has 'liked'.,A user access token with user_likes permission is required to see all pages liked by that person.

GET / v12 .0 / me / likes HTTP / 1.1
Host: graph.facebook.com
/* PHP SDK v5.0.0 */
/* make the API call */
try {
   // Returns a `Facebook\FacebookResponse` object
   $response = $fb - > get(
      '/me/likes',
      '{access-token}'
   );
} catch (Facebook\ Exceptions\ FacebookResponseException $e) {
   echo 'Graph returned an error: '.$e - > getMessage();
   exit;
} catch (Facebook\ Exceptions\ FacebookSDKException $e) {
   echo 'Facebook SDK returned an error: '.$e - > getMessage();
   exit;
}
$graphNode = $response - > getGraphNode();
/* handle the result */
/* make the API call */
FB.api(
   "/me/likes",
   function(response) {
      if (response && !response.error) {
         /* handle the result */
      }
   }
);
/* make the API call */
new GraphRequest(
   AccessToken.getCurrentAccessToken(),
   "/me/likes",
   null,
   HttpMethod.GET,
   new GraphRequest.Callback() {
      public void onCompleted(GraphResponse response) {
         /* handle the result */
      }
   }
).executeAsync();
/* make the API call */
FBSDKGraphRequest * request = [
   [FBSDKGraphRequest alloc]
   initWithGraphPath: @ "/me/likes"
   parameters: params
   HTTPMethod: @ "GET"
];
[request startWithCompletionHandler: ^ (FBSDKGraphRequestConnection * connection,
   id result,
   NSError * error) {
   // Handle the result
}];
load more v
75%

Hi, In my recent grails project, i was working on creating Apps for Facebook Fan Page and needed to show data to the user in such a way so that if he/she has “Liked” the page, then the content would be different from the case when the user has not liked it.,Way to check if user has “Liked” the Facebook Fan Page or Not,In simple words, if user is Fan of any Facebook Page, then the content of App Tab would be different from the user who is not Fan of that Page.,I searched a lot about it, and came across various solutions suggesting using JavaScript way to handle it but they didn’t worked for me, because although the User is logged in the facebook, but has not authorized the App and hence, we cannot fetch user_id of that user from facebook session.

Hi,
In my recent grails project, i was working on creating Apps for Facebook Fan Page and needed to show data to the user in such a way so that if he/she has “Liked” the page, then the content would be different from the case when the user has not liked it.

load more v
40%

To check whether a particular user likes your Page or App, you can use this Graph API call /me/likes, after the user is logged in to your application. You need the page id or application id to check against.,Without this permission you can still query user likes but only if the user has set user_likes preference to be accessible to public. ,Then, check the response.data array and compare the data.id with your application or page id.,In this example, 163504083699380 is my application id. I then iterate every user Like and check their id against my application id. If my application id is found on the list, then I know that the user likes my page.

In a nutshell, here’s what you need to do:
First, call the /me/likes API.

FB.api('/me/likes', onLikeCheckResponse);
load more v

Other "check-undefined" queries related to "How to check IF user has ALREADY liked the facebook page?"