How to know if my page is running in a facebook iframe or not

后端 未结 6 2028
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 09:51

I\'m currently developing a site which runs standalone and as a facebook app on an iframe I was wondering what whold be \"best practice\" for checking if my page is ran in a

6条回答
  •  旧时难觅i
    2020-12-30 10:50

    There are a couple of ways to approach this. If you're not concerned about security (i.e. you really only want to know how to format the page rather than deciding what content to show) then your best bet may be to use a distinct url for Facebook access. For example if your standalone site is www.mysite.com you can configure fb.mysite.com or www.mysite.com/fb to point to the same place, then use the alternate version in your app settings. Your server code can then easily check which url version is being accessed and act accordingly. Of course you have to take some care with your links to make sure they maintain the correct prefix.

    Another way is to use signed_request as discussed, setting a cookie (or session) when it is present to indicate a Facebook access. The trick there is to also include a bit of javascript code at the top of each page that checks to make sure the page is within an iframe. If not, then the code immediately redirects back to the current page with a parameter added like "?clearfb=1" which will tell the server to clear the cookie/session and output the page in external format.

提交回复
热议问题