简体   繁体   中英

Redirect to facebook app through app-website

I have made an app as a Page Tab. I need to redirect to the facebook-app if someone opens the website url directly. For that, I need to check the content of the address bar and if it doesn't contain www.facebook.com inside it, I will redirect to the facebook-app url. But, the problem is that inside the facebook iframe I am unable to get the content of the address bar.

Can you please tell me a good way to enable redirect to the fb-app in case someone accesses my website url directly?

Probably the easiest way to achieve this, is to do next check in JavaScript :

if (window.top !== window){
  window.location = 'http://facebook.com/YOUR_PAGE?sk=v_YOUR_APP_ID'
}

If you are wanting to redirect to a tab, a more complete way of doing this is checking for the 'page' parameter that is only sent through in the signed request for a page tab.

The reason being that only checking for the signed request will allow users to access your 520px tab app in a Facebook canvas (ie https://apps.facebook.com/yourapp which may not be what you want.

So for example

    $fb = new Facebook( array(
                          'appId' => <your_app_id>, 
                          'secret' => <your_app_secret>); 
    $sr = $fb -> getSignedRequest();

And then redirect the user as they hit the page

    if (!($sr['page'])) {
        die('<script>window.top.location = "<your_tab_url>"</script>');
    };

Facebook posts a signed request parameter when you open an app in fanpage.

You can check if that parameter is null or not.

In php,

if(isset($_REQUEST["signed_request"]){
// Opened in facebook
}else{

//opened outside facebook.
}

In asp.net

    if(Request.Form["signed_request"] !=null)
    { 
      // Opened in facebook
}
else
{
// opened outside facebook.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM