Multi-Tab Template for Facebook Timeline Page Tabs!
Our TURNKEY template has all the code and files to create a website-like Page Tab.
See it in action, and learn more....

Customizing your Facebook iFrame Application – Reveal Tabs Fan-gating & Other Cool Stuff

If you’re NOT on the HyperArts Blog, CLICK HERE!

IMPORTANT: You have to upgrade your Fan Page to the new layout before the application below will work on your Fan Page. Many Pages will see a big banner at the top of their Page announcing the upgrade. If you don’t, you can click here to upgrade your Fan Page.

In our previous tutorial on Facebook iFrame tab applications, we looked at how to set custom iFrame tabs on your Facebook Fan Page.

In this tutorial, we will look at how you can use the information that Facebook passes to your iFrame application when it is loaded to customize the experience of the person viewing the tab. This includes presenting different content for fans vs. non-fans, aka the Reveal tab or Fan-gating.

NOTE: A basic understanding of PHP is required

This tutorial will require that you have at least a basic understanding of PHP. If you do not, you can certainly copy the code from this tutorial and use it, but it may be difficult for you to modify the code for your exact needs. We cannot provide support for PHP issues that you may experience since they are outside the scope of this blog.

There are lots of great resources about PHP on the Internet that can help get you up to speed. I have found the resources at Tizag to be useful as I learned PHP, so you might check them out.

Facebook’s new iFrame Page Tabs — the signed_request variable

When a Facebook user clicks on your “tab” in the left-column navigation, Facebook loads your page via an iFrame, which embeds your content into the Facebook page. While Facebook is loading your page, it POSTs some information about your page to your application index page.

The information that is POSTed is contained in an encoded variable called “signed_request”. When you decode the signed_request via PHP, the following information will be able to you:

  • The Page ID for the tab that is loading your application;
  • Whether the logged-in user has “liked” this Page;
  • Whether the logged-in user is an admin of the Page;
  • The country of the user;
  • The locale of the user.

Decoding signed_request data with Facebook’s PHP SDK

In this article we will be looking at how to handle the decoding of the signed_request data using the Facebook PHP Software Developer Kit (SDK) . If your server doesn’t support PHP, you may be able to accomplish something similar using the JavaScript SDK, but that is beyond the scope of this blog post.

Installing the Facebook PHP SDK

You can download the latest Facebook PHP SDK here.  Once you have downloaded the file, unzip it (if your OS didn’t already).  The file that we need is in the “src” directory and is called “facebook.php”.  We recommend that you put this file on your server in the same directory your index file resides.

Loading the Facebook PHP SDK in your index file

Next, you need to tell PHP to load the Facebook SDK file when your index file is loaded.

Open your index.php and add this PHP code:

<? require 'facebook.php';

Note: If you called your index file “index.html” based on our earlier iFrame Page Tab tutorial, you will need to change the name to “index.php” and also change the file name in your Facebook Application settings (Facebook Integration > Page Tabs > Tab URL).

Initializing the Facebook PHP SDK

Next, you will need to initialize the PHP SDK. To do this, insert some additional code into your PHP:

$app_id = "APP_ID_HERE";
$app_secret = "APP_SECRET_HERE";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));

Note: You will need to replace the “app_id” and “app_secret” values with the ones for your application.  You can get these by going to facebook.com/developers.  Under “My Apps” on the right side of the screen, click on the name of your application. You will see the App ID and App Secret values as shown below.

Using the PHP SDK to decode the signed_request variable

We are finally ready to decode the signed_request variable that Facebook POSTs to your application.

$signed_request = $facebook->getSignedRequest();

We can now access specific values in the array like this:

$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];

You might want to add some “echo” code to display the values of each setting for troubleshooting:

echo "<br>page id = $page_id";
echo "<br>page admin = $page_admin";
echo "<br>like status = $like_status";
echo "<br>country = $country";
echo "<br>locale = $locale";

Using the signed_request variables to create the Reveal / Fan-gating feature

Now comes the fun part. The most common thing you might want to do is present different content to people who have “liked” your page vs. people who have not yet liked it.

You can use the variables in signed_request to check if the visitor has liked the Page on which your iFrame tab resides. You do this by testing the value of the variable “like_status”:

if ($like_status) {
echo "You like us";
}
else {
echo "You don't like us yet";
}

Create Page Tab content based on which Fan Page the app is installed

Another common use of these variables will likely be to customize an application based on which Fan Page it is installed on. This will allow you to use a single application on multiple Pages by tailoring the output to a specific Page.

You can test the Page ID like this:

if ($page_id == '12345') {
echo "content for 12344";
}
elseif ($page_id == '56789') {
echo "content for 56789";
}

You can do similar tests to customize the content based on country or locale or to display specific content only for Page Admins.

Can I access user data?

Facebook does not pass any personal information about the logged in user to your application unless they have specifically authorized your application.  If they have authorized your application, their User ID will be passed as part of the signed_request and you can access it in the same way that we accessed the other values above.  Creating the Request for Permissions dialog is beyond the scope of this tutorial, but we might cover that in a future tutorial if there is interest.  Once you have the user ID, you will be able to access whatever information the user gave you access to via the Graph API.

Download the code

We have created a ZIP file with the sample code from this tutorial as well as the latest version of the PHP SDK file (facebook.php). You can download it here.

Troubleshooting

We will update this section with iFrame app troubleshooting apps as the discussion continues in the comments.

Check your protocol (HTTP vs HTTPS)
There is a known issue where iFrame applications contained on Fan Page Tabs appear blank when accessed over SSL (via a URL that begins with https:// rather than http://). This normally happens when a FB user turns the setting at Account Settings > Account Security > Secure Browsing (https) on. It can also happen if you the user goes to https://facebook.com rater than http://facebook.com. The expected behavior is that users accessing the tab over https should see the page listed in the Secure Canvas URL in the Application Setup. The bug is that the page is just blank instead. You can read more, and vote for, the bug at http://bugs.developers.facebook.net/show_bug.cgi?id=15200

iFrame content not visible when you are not logged in to Facebook
There was a known Facebook bug that sometimes caused iFrame app tabs to appear blank when the visitor is not logged into Facebook.

Update: 3/1/11 Most reports seem to indicate that this bug is now resolved in production.

The Facebook SDK requires PHP 5
In order to use the PHP SDK (aka facebook.php), you will need to have PHP5 installed on your server. If you are not sure what version of PHP you have, put this in an empty index.php file:

<? phpinfo() ?>

Check your URL’s for www
Some servers are set to redirect “domainname.com” to “www.domainname.com”. If you server does this and you specify “domainname.com” in your Canvas URL in App Setup, the redirection will erase the signed_request parameter and all of your variables will be blank. To get around this, just include the “www” in the Canvas URL. See our previous tutorial for help on application setup.

Next Steps

This is just a hint of the powerful new things you will be able to do with iFrame apps on your Fan Page. We hope that you find this useful — we would love to hear about what topics you would like to see covered in future posts in this tutorial series. Just add a comment to the post to let us know!

Technorati Tags: , , , ,

  • Peter L

    I have tried the above but I find that I am not receiving signed_request at all – $_REQUEST is always empty.

    Does this work for others?

    • http://www.facebook.com/bentouch Benjamin Nadaud

      Did you find any solution? got same issue :(

  • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

    Hi Peter,

    If your application is being loaded into a Fan Page Tab using an iFrame, the signed_request should always be POSTed to your index page. You might want to use Firebug if you are familiar with it. You can see the POST that Facebook makes to your iFrame there.

    Bill

    • Peter L

      Hi Bill,

      Take a look here: http://apps.facebook.com/hlusandbox/

      That is a direct copy of the above example. Firebug etc. and dumping all variables always says everything is empty.

      Another example (slight variation) which first dumps $_POST & $_GET, and second gets the signed_request as per the example above is at:

      http://www.facebook.com/hallyuthai?sk=app_165953813453713

      • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

        Hi Peter,

        I think that I see what the issue might be. It appears that Facebook is doing a POST to your page but is being given a “301 Moved Permanently” response (you can see on the Net tab of Firebug), which redirects it back to your page using the GET command, wiping out the variables that Facebook is passing to you. Maybe something in your .htaccess file is redirecting pages with the word “tab” in them?

        Bill

        • Peter L

          Hi Bill,

          Thanks a lot for that help – made a mistake in the URL for the tab – thereof the ‘tab’ part, and as you discovered the URL for the canvas was set to a location with redirection.

          All fixed and working now!

  • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

    Peter L :

    Hi Bill,

    Thanks a lot for that help – made a mistake in the URL for the tab – thereof the ‘tab’ part, and as you discovered the URL for the canvas was set to a location with redirection.

    All fixed and working now!

    Great. Glad to hear is working. Something very similar happened to me when I was writing the tutorial, so that issue came to mind.

    Bill

  • http://www.jgvisual.com Jonathan

    Great article. I was able to use this article to pull content solely meant for those who haven’t liked my page.

    I would like to set the iFrame as my default landing tab, but I’ve noticed that if users aren’t logged in they just see a blank page. Is there a way to show the iFrame to users who aren’t logged in?

    Thanks a lot.

    Jonathan

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Jonathan,

      You ran across a current Facebook bug – when the user is logged out, they can not view iFrame apps on Fan Page Tabs (they just appear blank). I just added details in the Troubleshooting section above about how to find out more and to “vote” for Facebook to fix it.

      Bill

      • http://www.jgvisual.com Jonathan

        Thanks Bill. I commented on the bug on Facebook. Hopefully they’ll get this figured out soon.

        • http://www.facebook.com/HYVAssistant Owen McGab Enaohwo

          Okay guys so this is a bug and is indeed being worked on by Facebook or has it been fixed? I ask because I had my coder create an IFRAMEs tab for me and it worked but whenever I am not logged into Facebook it does not show anything. It is blank.

          Please advice

  • John Lawlor

    Below are the results of my echo statement from above:
    The text in the user id echo is the “like status”
    I get the same results for both “like” states. Thanks for the help.

    page id =
    page admin =
    like status =
    country =
    locale =
    user id = You don’t like us yet 6.41

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi John,

      Since all of the values are blank, the like/don’t like logic isn’t going to work correctly. I would suggest to double-check your App ID and App Secret values in the PHP file and make sure that the facebook.php file is in the same directory as your index file. If you are still getting blank values, it would be great if you could post the URL to your page/tab so that we might be able to help troubleshoot.

      Bill

  • John Lawlor

    Thanks for the rapid feedback — I used the wrong App ID and App Secret values. The new results:
    page id = 195014787192702
    page admin = 1
    like status = 1
    country = us
    locale = en_US
    user id = You like us 6.41

    J:L

  • John Lawlor

    On another note – I have tried to use php files hosted on Amazon S3 but have had no luck with FB iframes.

    Is there any way to use S3 with FB iframes?

  • John Lawlor

    I am a php novice that can’t work out how to display a “you like us image” to the “likes” and a “we live for likes” image when they haven’t liked the page –

    something like this:
    if ($like_status)
    SHOW IMAGE-1

    else
    SHOW IMAGE-2

    Thanks

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hey John,

      I don’t want to get too deep into PHP coding, but here is some basic code to do what you want:


      if ($like_status) {
      echo "<img src = 'you_like_us.jpg'>";
      }
      else {
      echo "<img src = 'please_like_us.jpg'>";
      }

      Hope that helps!

      Bill

      • http://www.webempire.in web empire

        Hi

        another solution for this is

        if ($like_status) {
        include("non-fans.html");
        }
        else {
        include("for-fans.html");
        }

        Make sure both html files are in the same directory where your index.php file resides.

  • http://www.club6400.com Les

    Thank you Bill!

    I have been searching for days literally to find some way of getting page variables for my app. This works PERFECTLY in PHP!!

    Unfortunately, I’m a die hard Classic ASP man and my apps are all Classic ASP. Anyone have any tips on converting this to Classic ASP or even a JavaScript version?

    • http://www.club6400.com Club 6400

      Just an update to my comment above, I was able to use this PHP solution to do what I needed and to force people to like. And what I had it do was if the person was a fan, the PHP would redirect to my app which is built in ASP. So, got it working by using the PHP to run the checks then ASP for the actual app. Thanks again Bill for this great post.

  • John Lawlor

    This little bit of code has saved several strands of hair tonight! Thank you for the great article and the code.

    J:L

  • http://www.web-media.co.uk Rob Willox

    Is it possible to make direct posts or some sort of scheduled post feed to a page which were only visible to ($like_status) visitors eg in the case of a weekly offer only for fans.

    Rob

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      I don’t believe that is any way to post items on the Wall for just fans of your page. You could build an application where you post your offers and then make that part of the page available only to fans (using $like_status), but they would have to visit the page to see the offers.

      Bill

  • http://www.braginteractive.com Brad

    Bill,

    Do you know of a way to have a lightbox or modal window work using the new iframes? I have tried it and the lightbox stays within the iframe..? I was hoping you had a solution.

    Thanks

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Brad,

      iFrames by their nature limit content to a specific area of the page. So as you found, you could do a ligttbox, but it has to fit within the iFrame and can’t overlap into the main Facebook page area.

      Bill

  • http://localdominate.com Local internet marketing as detailed by Phil Benham

    I have used the above and it WORKS! I love it! I don’t know much about PHP coding but I found out that you have to put all of your HTML in the echo "...<html>..."; format. Plus, you have to replace quotes with an apostrophe like so: echo "<link rel='stylesheet' type='text/css' href='styles.css' />";

    Pretty cool! I’m using html5 with java scripting for my facebook page. So in essence my facebook page is putting up an html5 page (w/ html5 video, btw) that uses javascript to pull another web page in…so it is three deep! Plus having the like vs. not liked functionality is expansive!

    I am having a problem though that I hope you coders can solve…if a non-fan likes my page using the downloaded script here, they still have to refresh the page to see the liked content. Is that a facebook bug; or can it be fixed with code?

  • http://localdominate.com Local internet marketing as detailed by Phil Benham

    Actually, it may be that your iframe has to propagate throughout Facebook’s servers since it works flawlessly now; simply after waiting for a while.

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Phil,

      Glad to hear you got it working! And thanks for the PHP tips — I’m sure they will he helpful for a lot of people reading this tutorial.

      Bill

  • Ryan W

    Thanks for the posts! I have the app all set up with the facebook.php in the same directory as my index.php. I keep getting this parse error:

    Parse error: syntax error, unexpected T_NEW in /nfs/c01/h12/mnt/5825/domains/website.net/html/iframe/facebook.php on line 4

    Any ideas?

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Ryan,

      I believe that the Facebook SDK requires PHP5. Are you perhaps using PHP4 on your server?

      Bill

      • Ryan W

        Thanks Bill. Upgraded to 5.2.14 and looks like everything is working!

        Thanks again!

  • Alex

    any idea how to add a facebook “Like” button inside the iframe (for those that dont like us yet) and have it so that after they use that Like button the iframe shows the “you like us” content??

    Here is the code I have so far


    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script&gt;
    <script type="text/javascript">
    window.fbAsyncInit = function(){
    FB.init({
    appId:'<?= $FB_appId ?>',
    session:{},
    status:true,
    cookie:true,
    xfbml:true
    });
    FB.Event.subscribe('edge.create',
    function(response){
    top.location.href = 'http://www.facebook.com/WebmasterCMS?sk=app_&lt;?= $FB_appId ?>';
    }
    );
    };
    </script>

    <fb:like
    href="http://www.facebook.com/WebmasterCMS&quot;
    layout="button_count"
    show_faces="false"
    width="120">
    </fb:like>

    for some reason it does not reload the page as “liked”

  • Raisun John Sunny

    I created a new app, and followed your instructions to add it to my fan page.
    These are my problems:
    1. When i click on the tab it redirects to the http://apps.facebook.com/i_want_to_quit/ and then for some reason again redirects to the site in which the files are kept http://iquit.co.nr/iquit/

    2. Second no values are displayed
    page id =
    page admin =
    like status =
    coutnry =
    locale =

    3. I tried changing the tab name to “I wnat to quit”….but it still shows “My Custom Tab”.

    Please help as it is important.

    Here are some of the details u might require.
    Site URL http://iquit.co.nr/iquit/
    Canvas URL http://iquit.co.nr/iquit/
    Canvas Page http://apps.facebook.com/i_want_to_quit/

    I have edited it a hundred times, but now it does not allow me to edit the app settings. Nothing seems to change.
    Please help.

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Raisun,

      Can you post the link to your Facebook Fan page to help with troubleshooting?

      Bill

      • Raisun John Sunny

        Hi Bill
        I apologize for a mistake I made in my statement.
        I mistakingly wrote fan page. I am actually trying to launch the app from my “application profile page”. From there when I launch the app it starts redirecting. Also clicking on the Go to App gives the same result. As of now its still under construction and I haven’t added it to any fan page.
        The same thing happens when you use the Canvas Page.
        Don’t know what I am missing here.
        Please help if possible
        With Regards
        Raisun

        • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

          Hi Raisun,

          I’m afraid that I’m not clear on the issue. The canvas URL above returns a “not found” error. This tutorial is really about how to add a tab to a Fan Page. You might want to try the general Facebook Developer Forums at http://forum.developers.facebook.net/ to see if somebody can assist.

          Bill

  • http://www.web-media.co.uk Rob Willox

    Hi

    Used the code file supplied and added some Liked and non-Liked content and the page displays the non-liked content ok but all the time.

    The check output shows the following even to me. As the admin I have also liked the page and show in list but it would appear not to be picking up the link status (see below echoed output) to display alternative content:-

    page id = 105005222880063
    page admin = 1
    like status =
    coutnry = gb
    locale = en_GB

    Is it because liked the page before upgrade although have unliked and liked again with no change.

    Anyone else seeing this?

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Rob,

      The variable $like_status should be set to “1″ if you have liked the page where the application is installed. You might want to double-check your PHP code that is setting that variable.

      Bill

      • http://www.web-media.co.uk Rob Willox

        Hi Bill,

        The PHP code is a direct copy of the sample file in your download:-

        $page_id = $signed_request["page"]["id"];
        $page_admin = $signed_request["page"]["admin"];
        $like_status = $signed_request["page"]["liked"];
        $country = $signed_request["user"]["country"];
        $locale = $signed_request["user"]["locale"];

        Took whole file and dropped in html for non liked status. Can’t see anything wrong with it!

        Here’s the page with echoed status:-
        http://www.facebook.com/pages/webmedia/105005222880063

        Can you test it?

        regards
        Rob

        • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

          Hi Rob,

          It looks like you fan page hasn’t been updated to the new format yet. Check out the “Important Note” at the top of the tutorial to get that set up and then the application should work. Once you page is upgraded, the tabs will be on the left, which is an easy way to tell the old format from the new format.

          Bill

      • http://www.web-media.co.uk Rob Willox

        Hi Bill

        Just done as you suggest and it is fine now! Thanks for testing!

        Thought had upgraded as when got the original notice used the link provided and was able to Use Facebook as Page.

        The tabs were showing on the left so assumed all had gone to plan and after March 12 would become the norm.

        regards
        Rob

        ps now just have to go and update the content!

      • http://www.web-media.co.uk Rob Willox

        Hi Bill

        Don’t know what is going on but after doing as suggested got the liked message but now when using Facebook as Page when the left tab link is clicked still getting non-liked data.

        Sorry, but could you check again and see what displays; there should be no image block showing.

        regards
        Rob

        • http://www.web-media.co.uk Rob Willox

          Hi Bill

          An update. It appears that if you are browing the page as a user, having liked the page, you get the correct data.

          However, if you click the link on right ‘Use Facebook as “PageName”‘ and then view the tab you are always given the like_status = 0 data.

          Is this a bug, as would have thought that as page owner/admin, and having liked the page using personal profile, you would get the like_status content when using page.

          Or, maybe I’ve just missed something!

          regards
          Rob

  • Markus

    Hello,
    Thanks for your great and easy description, got it up and running.

    i wonder why u write that …
    echo "<br>user id = $user_id";

    there is no user_id in the sent request!

    echo "pre"; print_r($signed_request);

    is it possible to not get only the page id, but also ONE hint what user is behind the visitor?

    Max

    PS: Facebook is crazy by itsself sometimes if u like and unlike a page, it not always shows the correct status (on all of the site’s pages it shows the “i like” button but if u click on it it shows a sign “u are already connected).

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Markus,

      You caught an editing error on our part. Facebook does actually pass the User ID as part of the signed_request, but only if the user has authorized your application. I decided to remove that part from the tutorial since displaying the Request Permissions dialog requires a lot more explaining.

      Bill

      • Markus

        Alright, thats what i thought ;-) without authorisation u cannot get any useful information (Language and country, except IP adress, etc..) regarding the visitor.. bad for data harvesters, good for users, so thumbs up :-)

  • http://microarts.com Michaleen Craig

    Joining the fun of learning how to set up custom tabs with iframes.

    I’m also running into this issue w/ no values showing up in the id fields:

    http://www.facebook.com/pages/MicroArts-Development/113528355326941?sk=app_132489196821678

    Any help is appreciated!

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Michaleen,

      It looks like your server is redirecting the POST that Facebook makes, which is causing the variables to get lost. It looks to me like they are trying to POST to worldsbestcatlitter.com but are being redirected to http://www.worldsbestcatlitter.com. Try changing your canvas URL to include the www part and hopefully the issue should be resolved.

      Bill

  • ikelleigh

    I’ve been testing this procedure with the test files provided above. The php files are hosted on a server that has PHP v4 (which could be the problem). I get a blank page, nothing on it. As a test, I added

    echo “Test php page for Facebook.”;

    And that shows up fine, but anything after that regarding the Facebook lines of code don’t show up.

    Any ideas?

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      The PHP SDK requires PHP5, so most likely is is causing an error on your PHP4 server, which is causing the blank page if error reporting is turned off.

      Bill

  • http://localdominate.com Local internet marketing as detailed by Phil Benham

    I just wanted to give you guys some examples I’ve been working on. Some likes would be super; since I am trying to get that all elusive domain name without the number…http://www.facebook.com/pages/LocalDominate/114166838625032 I hope this is okay since it is an example of what we are all sharing.

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Phil,

      Nice use of the Reveal function on an iFrame tab!

      Bill

    • http://localdominate.com Local internet marketing as detailed by Phil Benham

      Here's the link. Unfortunately, it did not render correctly.

  • Raisun John Sunny

    Bill Dailey :
    Hi Raisun,
    I’m afraid that I’m not clear on the issue. The canvas URL above returns a “not found” error. This tutorial is really about how to add a tab to a Fan Page. You might want to try the general Facebook Developer Forums at http://forum.developers.facebook.net/ to see if somebody can assist.
    Bill

    I tried but nobody cud. I have fixed the first problem. Let me explain a little bit. I am using a free web hosting 0fees.net. Unfortunately facebook did not allow to add this url. So I used co.nr to redirect to the 0fees.net. I guess that is why my values are blank. What can I do?

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Raisun,

      Yes, the redirect is probably the issue since it would wipe out the info that Facebook is POSTing to your page. You will probably need to get a web host that FB is OK with and then use the direct URL.

      Bill

  • http://microarts.com Michaleen Craig

    Bill – you are a genius – thanks so much for the help!

  • http://www.webempire.in Web Empire

    Hi

    I would like to drag your attention to a bug, we faced it while working for a client. If user is accessing facebook over secured https protocol he won’t be able to access the iframe app (tab using iframe) if you haven’t defined secured canvas in setting and in order to define secured canvas you should have https://… reference.

    Thanks

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Thanks for the tip. Do does it work for you when you define a https:// URL in the Secure Canvas URL field? I have seen comments online that it doesn’t work all the time so I’m curious if it works for you.

      Bill

      • Dave Bates

        There is another current show stopper bug that makes the iframe canvases not work with https at all. I am really mad about this since my contest is live right now and people can’t enter it. I couldn’t have ever worked the way they have it, so they must HAVE NOT EVER TESTED IT. Crazy! Who’s running things over there.

        • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

          Thanks for the info Dave. I just added info about this FB bug to the Troubleshooting section of this article. Hopefully everybody will “vote” for the bug so that it may get a bit more attention from Facebook.

  • http://www.saganwebdesign.com Sagan Website Design and Internet Marketing

    There is a missing semi-colon at the end of echo “content for 56789″…

    I understand this is something we replace with our own content, but, for testing it was frustrating to figure out why I was getting the error 500, until I noticed the missing ;

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Thanks for catching the code typo. I just updated it in the tutorial and fired my editor..

  • Michaleen Craig

    Bill –

    One more question. What are you doing for forms w/in iframes? Javascript validation? AJAX validation?

    Also – what do you NOW use to ‘share this tab’ – the same code you’d use on a website?

    Thanks!

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Michaleen,

      You can use any standard web page technologies within the iFrames so the usual JS should work fine.

      On the second question, Tim will be writing a post soon about how to incorporate all kinds of fun Facebook tags into your tabs. Stand by!

      Bill

  • Roderik

    Hi
    Thanx for the great example.
    I was wondering how easy it was to extend this example to make the user choose which var to send to the server.
    I will explain what i want

    I had a website with users. Now i want a facebook application, where a user can list the things he has on our website, on a facebook tab.
    The thing i have trouble with is suppliying the user with some sort of setting for the app where he can fill in his user id or name he uses on our website, so i know which list to retrieve.

    Hope you can help me.

    Thanx again for the example

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Roderik,

      When you have an iFrame app that you add to a Facebook Fan Page tab, you can put whatever content on that page and link it to other pages. So in your case, you could display some sort of login form on the index page for the application and then once the user logs in, you could show them customized content from your site.

      Bill

  • Simone Bonati

    I have some problem… the result of

    $signed_request = $facebook->getSignedRequest();

    is

    Array (
    [algorithm] => VALUE
    [expires] => VALUE
    [issued_at] => VALUE
    [oauth_token] => VALUE
    [profile_id] => VALUE
    [user] => Array (
    [country] => VALUE
    [locale] => VALUE
    [age] => Array (
    [min] => VALUE
    )
    )
    [user_id] => VALUE
    )

    where value is some value :) . I don’t have [page]… why? where i’m wrong?

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Simone,

      Are you accessing the application from a Fan Page tab rather than from apps.facebook.com? The page variable is only passed from Fan Page Tabs.

      Bill

      • Simone Bonati

        problem solved: facebook bug… after 5 hours the code run fine… tnx for help :)

  • http://www.web-media.co.uk Rob Willox

    In an earlier post it was stated that your html content should be placed within the double quotes of the echo statement and that to get it to display properly to convert all the double quotes in file to single quotes.

    Found there might be an issue when the html contained both double and single quotes particularly in some javascripts which might conflict with each other. It also made the index file quite unwieldy needing to cut and paste blocks of html.

    Tried using the php include() function with two individual files, one for each status, and it all works perfectly or at this stage appears to, even with mixed javascript quotes.

    It also means that the same index.php file can be used for all tabs with only the individual html tab file contents needing changed and update of the index.php app id and secret keys.

    Hope it might be of use to someone else.

    Rob

    • ikelleigh

      Thanks Rob. That’s helpful. I was trying the include() method was faced with several errors because the fan/non-fan pages were hosted on different servers than the php. So this is an alternate method I’ve used and it works perfectly:

      // Fan content
      if ($like_status) {
      $a = file_get_contents(“http://mysite.com/facebook/fan_special/index_fan.asp”);
      echo ($a);
      // Non fan content
      } else {
      $a = file_get_contents(“http://mysite.com/facebook/fan_special/index_nonfan.asp”);
      echo ($a);
      }

      • http://localdominate.com Local internet marketing as detailed by Phil Benham

        Sweet! It always helps when you have someone who knows code! Thank you guys for posting here! I’d be a fan…!

        • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

          Thanks for the great tips guys. Here is another way to showing two different HTML code blocks without having to deal with the quotes issue (if you don’t want to go the include file route):


          <?
          // Fan content
          if ($like_status) {
          ?>

          <p>all my content for people who like me go here</p>
          <p>I can have whatever HTML or JS content I want</p>

          <? }

          // Non fan content
          else { ?>

          <p>all my content for people who like me go here</p>
          <p>I can have whatever HTML or JS content I want</p>

          <? } ?>

  • ikelleigh

    Any reason why sometimes when I press the “Like” button the page refreshes and I see only the word “Success”?

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      I saw that a couple of times during testing but I’m not sure what causes it. You might use Firebug (www.getfirebug) to look at what Facebook is sending to your server and how your server responds to the POST to see if you can narrow it down.

      Bill

    • Chris

      The “success” message rarely happens to me but when it does, it leaves me wondering if this is a Facebook bug.

      I was browsing the Facebook forums and found a topic (http://forum.developers.facebook.net/viewtopic.php?pid=319871) where some other people where experiencing this problem, one of them opened up a bug report here:

      http://bugs.developers.facebook.net/show_bug.cgi?id=15495

  • http://localdominate.com Local internet marketing as detailed by Phil Benham

    I’m having so much fun with this, it’s CRAZY! You’ve got to check out my <a href="http://www.facebook.com/pages/LocalDominate/114166838625032?sk=app_194013563951175">Sample Page 2</a>!!! I’m using a reveal page with lead capture! And if you like the page, you’ll see a full-fledged mobile website. It’s coded as HTML5 and utilizes the Video JS platform for video…plays in every browser on every device! Can you just see how Facebook can be your new “landing” and sales pages? Notice the navigation menu…javascript. I’m using MailChimp on the reveal tab and a php script for the ‘contact us’ form on the mini-site. This is just going to be a barrel of fun!…I wish I knew php and javascript…I’d go nuts! Please visit even if it’s just to see all of the cool stuff…I need the ‘likes’! Thanks.

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Very nice. You are doing some cool stuff with the new functionality!

  • Lionel

    Hi,

    I’m interested to know how we can get UID of the user. I worked on since 2 days. And the only solution i found, it’s to call permission popup dialog. Not very beautiful like solution and some bugs with IE and chrome.

    Do you find a solution?

    Best regards

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Lionel,

      As you have found, Facebook doesn’t pass the User ID to your application unless the user has authorized your application. This is by design. I am planning to do a post about how to display the Request Permissions dialog using Javascript in the future, but it will be another week or so before that one is ready.

      Bill

  • http://www.dennyardian.com Denny

    Cool !, the code does fine. but how can I bring ‘auto-refresh’, if somebody like or unlike the tab, it gone blank until it refreshed.

    how we made the page refreshed itself ? or something similar..

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Denny,

      Sounds like you may have run across a bug there. If you Like or Unlike the page it should refresh automatically and show your content.

      Bill

      • http://dennyardian.com Denny

        Hi Bill,

        I try with chrome, and it works well, but with firefox, when I go from Unfan to Fan, no problems, but when I do Unlike, it comes blank.

        but no problem, since I don’t mind to much about the unfan part :D

        thanks anyway for the great tips :)

  • http://dennyardian.com Denny

    Denny :
    Hi Bill,
    I try with chrome, and it works well, but with firefox, when I go from Unfan to Fan, no problems, but when I do Unlike, it comes blank.
    but no problem, since I don’t mind to much about the unfan part
    thanks anyway for the great tips

    as additional, little weakness, not important though. the iFrame wont show up if the user isn’t login in facebook.

    http://www.facebook.com/pages/Pebisnis-Jalanan/110173965718508 (warning!, non-English :)

  • http://www.prosoncheng.com/ proson

    I would like to know how to get the “share with friends” button work on the Fanpage or Apps.

  • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

    Owen McGab Enaohwo :

    Okay guys so this is a bug and is indeed being worked on by Facebook or has it been fixed? I ask because I had my coder create an IFRAMEs tab for me and it worked but whenever I am not logged into Facebook it does not show anything. It is blank.

    Please advice

    Hi Owen,

    Yes, that is the bug described in the Troubleshooting section of this tutorial. Facebook is currently beta testing a fix and are they are hoping to roll it out in the next couple of days.

    Bill

  • http://www.carrcommunications.com David F. Carr

    Got this working as part of the Facebook Tab Manager plugin for WordPress, http://wordpress.org/extend/plugins/facebook-tab-manager/

    Thanks for your explanation, made this much easier to tackle.

  • Jim

    Denny – this is bug 15495 in the facebook bug tracker. It only has 4 votes so far – vote for it!

  • Polly Baker

    Can I ask, why do you think my page remains blank/empty. If you right click in the empty space and click “Show the Frame” it appears on the server page. My code below as well as the server link where I have the pages hosted. I have facebook.php, index.php, index1.php and index2.php all in the same directory/folder. Any help much, much appreciated!
    index.php

    <?php

    require 'facebook.php';
    $app_id = "143799205684208";
    $app_secret = "c4b2f53e1ff5e38a42ed8c284908fd25";
    $facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
    'cookie' => true
    ));

    $signed_request = $facebook->getSignedRequest();

    $page_id = $signed_request["page"]["id"];
    $page_admin = $signed_request["page"]["admin"];
    $like_status = $signed_request["page"]["liked"];
    $country = $signed_request["user"]["country"];
    $locale = $signed_request["user"]["locale"];

    if($like_status) {
    include("index2.php");

    }
    else {
    include("index1.php");
    }

    ?>

    The server host and Canvas URL I’m using (to see that the image does, in fact, appear there) is: http://www.lcmcm.com/testing/

    I just can’t figure out why the fan page is empty.
    Thanks so much for any help you might offer.

  • Liam

    Hi guys, great tutorial, very helpful. However, I’m at a loss to why my canvas is blank? http://apps.facebook.com/africaaway/

    The app itself was/is fine and in working order, but when the PHP (index.php & facebook.php) is included I get nothing. All files are located in the same folder and properly uploaded, I’ve double checked the app settings and everything seems to be in order – OAuth 2.0 is enabled. Server is running PHP 5.0.4. Here’s the code in index.php:

    <?php

    require 'facebook.php';
    $app_id = "(id is actually here)";
    $app_secret = "app secret is actually here";
    $facebook = new Facebook(array(
    ‘appId’ => $app_id,
    ‘secret’ => $app_secret,
    ‘cookie’ => true
    ));

    $signed_request = $facebook->getSignedRequest();

    $page_id = $signed_request["page"]["id"];
    $page_admin = $signed_request["page"]["admin"];
    $like_status = $signed_request["page"]["liked"];
    $country = $signed_request["user"]["country"];
    $locale = $signed_request["user"]["locale"];

    if ($like_status) {
    include("index2.php");

    }
    else {
    include("index1.php");
    }

    ?>

    • http://localdominate.com Local internet marketing as detailed by Phil Benham

      Try changing the “include” to:

      // If a fan is on your page
      if ($like_status) {
      $a = file_get_contents(“http://yourwebsite.com/facebook/home_page.html”);
      echo ($a);
      } else {
      // If a non-fan is on your page
      $a = file_get_contents(“http://yourwebsite.com/facebook/reveal.html”);
      echo ($a);
      }

      • Polly Baker

        Thank you, Phil, I appreciate your response here. I tried your code above and unfortunately, still no luck :-( Same as Liam, I have everything as it should be in the app (and if I right click in the blank area, I can open the frame in a new window), the OAuth 2.0 is enabled, facebook.php is in the same directory as index.php and the fan and non-fan pages, my server is running PHP Version 5.2.14.

        The tab name is “Welcome Page”
        http://www.facebook.com/pages/Lisa-Murphy-Example-Fan-Page/151790051544589?sk=app_143799205684208
        Server directory where I have files is: http://www.lcmcm.com/testing/index.php

        I’ve also gone in and taken out the www and then put it back…nothing changed. I’m lost for why it is not working :(

        • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

          Hi Polly,

          I just took a look at your Tab using Firebug (a Firefox extension) and it looks like when Facebook makes their POST to your page at http://www.lcmcm.com/testing/index.php, they are recieving a “500 Server Error” response from your server. Most likely there is some sort of error in your PHP file. If you have access to PHP error logs on your server, that might help in narrowing down the issue.

          Bill

    • Polly Baker

      Liam, your issue is identical to mine. I’ve tried everything….just the blank/white screen, but w/Firefox, if I right click, I can open the frame in a new window and it is going to the else statement, or index1.php but it is not appearing within the frame on Facebook. Thanks for your post. I am glad I’m not the only one with this same issue….driving me nutts :-)

  • Polly Baker

    Bill Dailey :
    Hi Polly,
    I just took a look at your Tab using Firebug (a Firefox extension) and it looks like when Facebook makes their POST to your page at http://www.lcmcm.com/testing/index.php, they are recieving a “500 Server Error” response from your server. Most likely there is some sort of error in your PHP file. If you have access to PHP error logs on your server, that might help in narrowing down the issue.
    Bill

    Thank you sooooo much, Bill. Issue resolved!! For future reference, and particularly for Liam above. The issue was two-fold. First, an error on the log brought up that I was missing the style.css. Next, and most important, Fatal error: Call to undefined function hash_hmac() in file location on server, line 771. I HAD TO TURN ON THE HASH MODULE which, for some reason, was set to off. Either your server company can do this or go to command line and vinstall php5 and toggle that module to on. My page is finally working. Made my day :-) Thanks, Bill, and hopefully this info I’m providing can help others too if they experience the same issue.

  • Guillermo

    Bill,

    Thanks for the great article.

    I am having a weird problem. My fan page recognizes if the person is a fan or not and displays the homepage accordingly.

    Within the homepage, the page tells visitor to click to download the coupon, if the person is a fan, they should see the coupon, if the person is not a fan it should ask them to like the page.

    Although the different versions are being displayed for the homepage, the Coupon page shows the Not-Fan version regardless…

    Help please!

    http://www.facebook.com/curtaincallinteriors?sk=app_152771908112353

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Guillermo,

      Facebook only makes a POST to your index page (as specified in the Application Setup). On that page, you can read the variables in the signed request as detailed in this tutorial. If you then link to another page, you would need to pass anything you need to know to the other page.

      For example, if you have determined that the visitor is a fan, you could pass that info in the URL like this:


      <a href = "coupon.php?fan=true">View coupon</a>

      The easier thing in this case might be to change the code on the index page so that the coupon button is only displayed when they are a fan.

      Bill

  • http://www.mmprint.com Giovanni

    This is great thanks for this info and it worked for our facebook page.

    I do have one issue. When a user is not logged in and clicks “Like” and is asked to login, well after login, they are redirected to my wall and not my “you like us” page.
    Any help would be appreciated.
    facebook.com/mmprint

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Giovanni,

      Not sure what is going on there. On the app pages I have created, when somebody clicks the “Like” icon at the top of the page, the page reloads and shows the “You like us” content. It doesn’t take them back to the “Wall”.

      Bill

      • http://www.mmprint.com Giovanni

        Bill Thanks for the prompt reply. Could it be caused by my use of “require” instead of “echo” or “include” ? Any ideas on what I can try differently?

        • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

          Hi Giovanni,

          I wouldn’t think so. It sounds more like it might be a Facebook bug. Anybody else seeing something similar?

          Bill

  • http://www.mmprint.com Giovanni

    Giovanni :
    This is great thanks for this info and it worked for our facebook page.
    I do have one issue. When a user is not logged in and clicks “Like” and is asked to login, well after login, they are redirected to my wall and not my “you like us” page.
    Any help would be appreciated.
    facebook.com/mmprint

    Forgot to mention:

    I have my index file setup to this:

    if ($like_status) {
    require ‘like.php’;
    }
    else {
    require ‘non_fan.php’;
    }

    and my facebook settings are set to display my app by default.

  • Octavian Morar

    Hello Bill,

    I have been using the information you provide in your articles for quite a while now. At this point I have a small problem that I could really use your help with.

    I am using Root Music’s BandPages application for one of the Faceboko Pages I am administrator on. At the same time I am using the Reveal Tab code you provided with this article.

    Everything works fine with the Reveal Tab, but I was wondering if it is possible to redirect the people to the BandPages Tab rather than revealing hidden content once they hit the Like button.

    Looking forward to your reply.

    Best.

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      Hi Octavian,

      You could use some JavaScript to redirect the page once the user has “liked” your page. Something like:


      <script language="javascript">
      window.top.location.href = "PUT_URL_TO_REDIRECT TO HERE";
      </script>

      Just put in the full URL for your BandPages app and it should work.

      Bill

      • Octavian Morar

        Hi Bill,

        It was exactly what I needed.

        Thank you.

  • http://www.oman-collective-intelligence.com Maurizio

    Hi Tim,
    i have a question, probably a stupid one. Can i add iFrames if i don’t have a web server? Could i use a hosting site?
    Thanks!

    • http://www.hyperarts.com/blog/author/bill-dailey/ Bill Dailey

      HI Maurizio,

      Not a stupid question. You will need to somewhere to store the files — it can be any kind of web hosting site that supports POST to a PHP/ASP file. We are actually working on a solution that might help if you don’t have your own web host/server — stand by for an announcement coming soon on that.

      Bill

      • http://www.oman-collective-intelligence.com Maurizio

        Thanks Bill, i look forward to the announcement then!

  • http://www.facebook.com/fastfanpages Paul Teague

    Firstly, I want to thanks you very much for posting this information, I really appreciate it, it’s saved me hours of figuring it all out.

    I hope you don’t mind me posting this here, but I created a ‘newbies’ guide as there were a few technical bits I didn’t quite get here at first …. this may just save somebody some time: http://www.facebook.com/fastfanpages#!/notes/fastfanpagescom/how-to-create-reveal-pages-using-iframes/115006318576998

    Once again, this is so helpful, many thanks for this information.

    Best wishes, Paul

  • Lenin Raj Rajasekaran

    Hello, I have a question. I have a text box and a submit button in my IFRAME app. When the ‘page admin’ submits this form, I want to send the form data to my server(where the app is hosted) and store the information in the db and later retrieve for non-admin users. How can we do that? Can you cover this in a blog post?

  • www.newsoftcom.com

    Thank you it works great. 

    All the best.

    Ivan