注销 Facebook(使用 PHP SDK)和 CodeIgniter

发布于 2024-11-11 02:45:56 字数 1054 浏览 2 评论 0原文

在开始实施 Facebook 身份验证之前,我的注销控制器如下所示:

# Kill the session
$this->session->sess_destroy();

# Redirect back to main page.
redirect('', 'location');

我尝试修改它以将用户也从 Facebook 注销,但没有成功。

# Kill the session
$this->session->sess_destroy();

require 'application/sdk/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '625256',
  'secret' => '25256256',
  'cookie' => true,
));

$session = $facebook->getSession();

if ($session) {
    $location = (string) html_entity_decode($facebook->getLogoutUrl(array('next' => 'http://localhost:8888/taketocollege/')));
    header("Location: $location");
    exit();
}

# Redirect back to main page.
redirect('', 'location');

我做错了什么?我不打算为此使用 Javascript SDK,因为我希望这一切都在控制器中完成。

谢谢!!

编辑:更清楚一点,问题是会话没有被破坏。我的注销控制器将用户发送到主页,但主页将具有会话的 Facebook 用户重定向到登录页面。因此 Facebook 用户最终会进入登录页面。通常登录页面会自动让他们登录,但是到那时,他们的会话就被破坏了。

Before I started implementing Facebook authentication, my logout controller looked like this:

# Kill the session
$this->session->sess_destroy();

# Redirect back to main page.
redirect('', 'location');

I tried modifying it to log the user out of Facebook as well, but no luck.

# Kill the session
$this->session->sess_destroy();

require 'application/sdk/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '625256',
  'secret' => '25256256',
  'cookie' => true,
));

$session = $facebook->getSession();

if ($session) {
    $location = (string) html_entity_decode($facebook->getLogoutUrl(array('next' => 'http://localhost:8888/taketocollege/')));
    header("Location: $location");
    exit();
}

# Redirect back to main page.
redirect('', 'location');

What am I doing wrong? I'm not looking to use the Javascript SDK for this since I want it all to be done in the controller.

Thanks!!

EDIT: To be a bit more clear, the problem is that the session isn't being destroyed. My logout controller sends users to the home page, but the home page redirects Facebook users with a session to the login page. So the Facebook users end up on the login page. Normally the login page would automatically log them in, but, by then, their session is destroyed.

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

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

发布评论

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

评论(2

硪扪都還晓 2024-11-18 02:45:56

这里有一个“注销”按钮,它指向您的注销控制器,并且您正在尝试让您的注销控制器调用某些内容以使用户从 Facebook 注销。

如果您使用 Facebook 登录用户,则不需要注销控制器。您只需在页面中显示注销 URL:

$args['next'] = "http://localhost:8888/taketocollege/";
$logoutUrl = $facebook->getLogoutUrl($args);
echo '<a href="' . $logoutUrl . '">Login with Facebook</a>';

如果您想在用户注销时清除会话,您需要在 next 页面上执行此操作(对于您来说,http://localhost :8888/taketocollege/):

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$user = $facebook->getUser();

if (!$user) {
    $this->session->sess_destroy();
}

希望有帮助!

What you have here is : a "Logout" button that points to your logout controller and you are trying to make your logout controller call something to make the user logout from Facebook.

If you log in your users with Facebook, you don't need logout controller. You just display the logout URL in your page :

$args['next'] = "http://localhost:8888/taketocollege/";
$logoutUrl = $facebook->getLogoutUrl($args);
echo '<a href="' . $logoutUrl . '">Login with Facebook</a>';

If you want to clear the session when the user logs out, you wan do it on the next page (for you, http://localhost:8888/taketocollege/) :

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$user = $facebook->getUser();

if (!$user) {
    $this->session->sess_destroy();
}

Hope that helps !

慕巷 2024-11-18 02:45:56

我的代码最初将用户重定向到主页。

$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);
  }
}

if($session && !$this->session->userdata('logged_in'))
    redirect('login');

该代码将用户重定向到登录页面,因为会话已更改,但并未完全删除。

通过更改最后两行,我能够解决这个问题:

if($me && !$this->session->userdata('logged_in'))
    redirect('login');

希望它可以帮助别人!

My code initially redirected users to the home page.

$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);
  }
}

if($session && !$this->session->userdata('logged_in'))
    redirect('login');

That code was redirecting users to the login page since the session had been changed, not completely erased.

By changing the last two lines, I was able to fix this problem:

if($me && !$this->session->userdata('logged_in'))
    redirect('login');

Hope it helps someone!

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