简体   繁体   English

Facebook php sdk session 逻辑

[英]Facebook php sdk session logic

How i maintain a session cookie in my facebook app?我如何在我的 facebook 应用程序中维护 session cookie? 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.据我了解,这是错误的,每次用户导航到我的 canvas 中的不同页面时,我都会得到一个新的 session。 Do i need to use request?我需要使用请求吗? if so how do i accomplish that?如果是这样,我该怎么做?

You're using the PHP SDK to fetch the user session, and that's okay.您正在使用 PHP SDK 来获取用户 session,这没关系。 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. 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.您确实需要在每个页面上进行安装重定向,以防您的用户 session 确实超时,或者如果用户在使用过程中卸载您的应用程序,或者在没有安装用户的情况下直接链接到您的应用程序页面。

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.为了使您的代码更简洁,您可以移动所有代码以获取 session 并将用户的安装捕获到单个脚本中,并将其包含在应用程序中每个页面的顶部。 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. 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.您可能还需要在您的应用程序中设置 P3P 策略 header,这通常可以解决该问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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