简体   繁体   中英

How can I stop my site from being shown within Facebook?

I'm using Facebook to manage the login for my website. I also use the PHP SDK to call the Graph API to retrieve friend lists, comments, posts etc.

The problem I'm having is that I want the site to be standalone, so that the user follows a link from Facebook and then comes to the site.

It kind of works, but sometimes when the user follows the link, they see my site's index page in the Facebook canvas area. I haven't fully managed to reproduce this, so haven't nailed down the circumstances in which it happens. however, when it happens, if I click 'Back' in the browser, I get taken back to my own site.

It looks like the link takes the user from Facebook to my site, which then calls Facebook to check that the user is logged in, but then it doesn't return to my site for some reason. but as I say, I'm not sure.

Anyway, anyone have any ideas what the problem might be?

My login code is:

// Create new Facebook object
$facebook = new Facebook(array(
  'appId'  => APP_ID,
  'secret' => APP_SECRET,
  'cookie' => true, // enable optional cookie support
));

// Create login url, in case user is not logged in or session has expired
$loginUrl = $facebook->getLoginUrl($params = array('canvas' => 0, 'fbconnect' => 0, 'req_perms' => 'publish_stream', 'cancel_url' => 'http://www.pubfish.com'));

// Get current facebook session
$session = $facebook->getSession();
$me = null;

// Session based API call.
if ($session) { // If session exists, make sure it's still valid
 try { // Get session to see whether it's valid
  // User is logged in and has authorised application already
  $uid = $facebook->getUser();
  $me = $facebook->api('/me');
 } catch (FacebookApiException $e) {
  echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
  error_log($e);
 }
}else { // No session - so ask user to log in
 $url=$facebook->getLoginUrl(array('canvas' => 1,'fbconnect' => 0));
 echo ('<script type="text/javascript">top.location.href=\''.$url.'\';</script>');
}

Have you tried setting 'next', like this: $params = array('canvas' => 0, 'fbconnect' => 0, 'req_perms' => 'publish_stream', 'cancel_url' => 'http://www.pubfish.com', 'next'=>'http://www.pubfish.com')? If all else fails you can use this to break out of the canvas iframe:

<script>
$(document).ready (function () {
if (window != top) top.location.href = location.href;
});
</script>

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