2

i want the codes which will redirect the user to my app if he/she tries to open my app hosting site. for ex: my app link: https://apps.facebook.com/gaming-zone/ app hosting link: http://gaming-zone.herokuapp.com/

i have found some codes already but those codes redirects the user continously and if he is using my app on facebook and he is playing any game then also it redirects the user to app home page. i want the exact codes that redirect the user only when he visits my hosting site.

These are the codes that i found already:

<script type='text/javascript'>
if (window.top.location == window.location) 
  window.top.location = 'https://apps.facebook.com/gaming-zone/';
</script>

2 Answers2

0

The problem is that you cannot read the window.top.location value. You can only write it. Strange but thats it.

Option 1: Mount your app to a page tab and create a like-gate. Like gate will return false outside facebook iframe ALWAYS.

Option 2: Check signed request data when loading the page at backend.

Option 3: Other option is to examinate the top url as you do but at backend.

zsitro
  • 1,812
  • 3
  • 24
  • 34
  • zsitro thnx for your response can you please tell me the option 2 and 3 how can i do that plz provide me the codes :) – Mubbasher Ahmed Qureshi Sep 13 '12 at 14:15
  • Option 2: you can find resources here: https://developers.facebook.com/docs/authentication/signed_request/ and here: http://stackoverflow.com/questions/8229986/situation-with-signed-request-sessions-and-php-sdk-3-0 Option 3: heres how u can read parent url: http://stackoverflow.com/a/8962276/1178072 I suggest to print out the result and examinate it. Good luck! – zsitro Sep 13 '12 at 20:41
0

Here is the code that I use:

<script type="text/javascript">
  function NotInFacebookFrame() {
    return top === self;
  }
  function ReferrerIsFacebookApp() {
    if(document.referrer) {
      return document.referrer.indexOf("apps.facebook.com") != -1;
    }
    return false;
  }
  if (NotInFacebookFrame()) {
    top.location.replace("https://apps.facebook.com/YOUR_APP_NAMESPACE");
  }
</script>

This checks if they are currently in the Facebook frame and ONLY if they aren't it will redirect them to "https://apps.facebook.com/YOUR_APP_NAMESPACE"

Note: you can use this on any page in you app just change the URL accordingly.

For example: If you are using this in http://YourDomain.com/anotherpage.php you can change the URL in the code above to https://apps.facebook.com/YOUR_APP_NAMESPACE/anotherpage.php

Ariel
  • 166
  • 1
  • 3