PHP Facebook 会话管理

发布于 2024-12-15 08:24:19 字数 1386 浏览 1 评论 0原文

我正在使用 PHP 开发 Facebook 应用程序。 我可以使用下面的代码登录

 <?php

$app_id = "XXXXXXXXXXXXXXXXX";
$canvas_page = "http://apps.facebook.com/sandysfirst/";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
       . $app_id . "&redirect_uri=" .urlencode($canvas_page)."&scope="."publish_stream,read_stream,offline_access";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
       echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {

}?>

<?php
  include_once "src/facebook.php";
  $app_id = 'XXXXXXXXXXXXXXXXXXX';
  $application_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  $facebook = new Facebook(array(
 'appId'  => $app_id,
 'fileUpload' =>true,
 'secret' => $application_secret,
 'cookie' => true, // enable optional cookie support
));
       if ($facebook->getSession()) {
       $user = $facebook->getUser();
       $uid = $facebook->getUser();
              $fbme  =   $facebook->api('/me');
              echo "<br/><br/><br/><br/> Welcome ".$fbme['name'];
         }
       echo "<html>

,并且下面的 HTML 带有表单操作。 我想知道提交表单后如何在第二页上获得相同的用户 ID 或会话。 我没有使用 sdk 3.0 的 facebook.php 。

I am developing a facebook app using PHP.
I am able to login using the below code

 <?php

$app_id = "XXXXXXXXXXXXXXXXX";
$canvas_page = "http://apps.facebook.com/sandysfirst/";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
       . $app_id . "&redirect_uri=" .urlencode($canvas_page)."&scope="."publish_stream,read_stream,offline_access";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
       echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {

}?>

<?php
  include_once "src/facebook.php";
  $app_id = 'XXXXXXXXXXXXXXXXXXX';
  $application_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  $facebook = new Facebook(array(
 'appId'  => $app_id,
 'fileUpload' =>true,
 'secret' => $application_secret,
 'cookie' => true, // enable optional cookie support
));
       if ($facebook->getSession()) {
       $user = $facebook->getUser();
       $uid = $facebook->getUser();
              $fbme  =   $facebook->api('/me');
              echo "<br/><br/><br/><br/> Welcome ".$fbme['name'];
         }
       echo "<html>

and the HTML follows below that with a form action.
I want to know how I can get the same user id or session on the second page once the form is submitted.
I am not using facebook.php of sdk 3.0.

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

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

发布评论

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

评论(1

梦年海沫深 2024-12-22 08:24:19

我不知道 facebook api,但是从一些基本的 php 来看,您可以使用以下几种方法:

1 - 将 uid 添加为表单中的隐藏字段。

<input type="hidden" name="uid" value="$uid">

2 - 将其添加到操作 url

<form action="submit.php?uid=$uid">

然而,更好的方法是使用 facebook api,几乎与您已经在那里列出的方式相同(来自 include_once "src/facebook.php";),每次都检索 uid,否则您将面临安全黑客攻击(很容易在表单中传递虚假 uid)。

I don't know the facebook api, but from some basic php there's a few methods you could use:

1 - Add the uid as a hidden field in the form.

<input type="hidden" name="uid" value="$uid">

2 - Add it to the action url

<form action="submit.php?uid=$uid">

However the better way is to use the facebook api, pretty much the same way you've got it listed there already (from the include_once "src/facebook.php";), to retrieve the uid each time, otherwise you leave it open to security hacks (easy to pass a fake uid in a form).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文