Facebook php sdk 会话逻辑

发布于 2024-11-05 09:33:53 字数 1359 浏览 0 评论 0原文

如何在我的 Facebook 应用程序中维护会话 cookie? 目前我的所有页面都有以下代码:

$facebook = new Facebook(array(
  'appId'  => 'myid',
  'secret' => 'mysecret',
  'cookie' => true,
));


$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('canvas' => 1,
                                          'fbconnect' => 0,
                                          'req_perms' => 'publish_stream,email',
                                          'next' => 'register.php',
                                          'cancel_url' => 'http://www.facebook.com/apps/application.php?id=MYAPPID&perms' ));
}

if (!$me){ print("<html><head>");
print("<script type=\"text/javascript\">function redirect(){ top.location.href = \"");

print($loginUrl);
print("\"; }</script></head>");
print("<body onload=\"redirect();\">Please wait...</body></html>"); exit(); }

据我了解,这是错误的,每次用户导航到画布中的不同页面时,我都会得到一个新会话。我需要使用请求吗?如果是这样我该如何做到这一点?

How i maintain a session cookie in my facebook app?
currently all of my pages have the code below:

$facebook = new Facebook(array(
  'appId'  => 'myid',
  'secret' => 'mysecret',
  'cookie' => true,
));


$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('canvas' => 1,
                                          'fbconnect' => 0,
                                          'req_perms' => 'publish_stream,email',
                                          'next' => 'register.php',
                                          'cancel_url' => 'http://www.facebook.com/apps/application.php?id=MYAPPID&perms' ));
}

if (!$me){ print("<html><head>");
print("<script type=\"text/javascript\">function redirect(){ top.location.href = \"");

print($loginUrl);
print("\"; }</script></head>");
print("<body onload=\"redirect();\">Please wait...</body></html>"); exit(); }

This is wrong as i understand, i'm getting a new session each time a user navigate to a different page in my canvas. Do i need to use request? if so how do i accomplish that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2024-11-12 09:33:53

您正在使用 PHP SDK 来获取用户会话,这没问题。 PHP SDK 足够智能,可以在用户上设置和利用 cookie 来保存其会话信息 - 因此您不会在每次页面加载时从 Facebook“获取”任何新内容。您确实需要在每个页面上进行安装重定向,以防万一您的用户会话超时,或者用户在使用中卸载您的应用程序,或者在没有安装用户的情况下直接链接到您的应用程序页面。

为了使您的代码更简洁,您可以移动所有代码来获取会话和会话。将用户的安装捕获到单个脚本中,并将其包含在应用程序中每个页面的顶部。这样您就不会再有那些浮动的东西,并且您可以在需要时轻松更改/调整它。

编辑:如果您的用户每次导航到应用程序中的不同页面时都会丢失会话,则用户的浏览器可能会阻止 FB PHP SDK 设置的 cookie,因为它位于 iframe 中。您可能还需要在应用程序中设置 P3P 策略标头,这通常可以解决该问题。

<?php
header('P3P: CP="CAO PSA OUR"');

You're using the PHP SDK to fetch the user session, and that's okay. The PHP SDK is smart enough to set and utilize a cookie on the user which holds their session information - so you're not 'fetching' anything new from Facebook on every page load. You do need the redirect for install on every page just in case your user session does time out, or if the user uninstalls your app mid-use, or gets a direct link into a page of your application without being an installed user.

To make your code a little cleaner, you could move all the code to fetch the session & capture the installation from the user to a single script, and include it at the top of each page in your application. Then you don't have all that floating around, and you can easily change/tweak it when and if you need to.

edit: If your users are losing a session every time they navigate to a different page within your application, the user's browser might be blocking the cookie that the FB PHP SDK is setting because it's in an iframe. You probably need to set a P3P Policy header in your application, also, and this often fixes that problem.

<?php
header('P3P: CP="CAO PSA OUR"');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文