如何在 PHP 和 Zend 中使用 Facebook Connect 注销用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1386557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log out users using Facebook Connect in PHP and Zend?
提问by Abhinav
I am trying to build a Connect app using PHP and the Zend Framework. I also have a Zend_Auth based user authentication system. Now, I am able to log in using Facebook but log out is not working.
我正在尝试使用 PHP 和 Zend 框架构建一个 Connect 应用程序。我还有一个基于 Zend_Auth 的用户认证系统。现在,我可以使用 Facebook 登录,但注销不起作用。
I need to clear the Zend_Auth identity as well as remove all Facebook login info. What would be the best way to do this?
我需要清除 Zend_Auth 身份并删除所有 Facebook 登录信息。什么是最好的方法来做到这一点?
I tried facebook_client->expire_session()and facebook_client->clear_cookie_state();together and also facebook_client->logout($next)after calling Zend_Auth::getInstance()->clearIdentity()
我一起尝试过facebook_client->expire_session(),facebook_client->clear_cookie_state();也在facebook_client->logout($next)打电话之后Zend_Auth::getInstance()->clearIdentity()
None of them seem to work.
它们似乎都不起作用。
回答by typeoneerror
You have to call the javascript client logout first, then send them to your php logout script. So, call .js:
你必须调用JavaScript客户端退出第一,然后将它们发送到你的PHP脚本注销。所以,调用 .js:
FB.Connect.logoutAndRedirect("/path/to/zend/logout/controller");
You'll see a modal popup that says "you are logging out of this site and facebook* You'll be redirected to wherever your logout script is:
你会看到一个模态弹出窗口,上面写着“你正在退出这个站点和 facebook* 你将被重定向到你的退出脚本所在的任何地方:
try
{
$facebook->expire_session();
}
catch (FacebookRestClientException $e)
{
//you'll want to catch this
//it fails all the time
}
I usually also call this function in the PHP logout script, just to be safe:
为了安全起见,我通常也在 PHP 注销脚本中调用此函数:
/**
* Remove the local Facebook cookies that get set manually
*
* @return void
*/
protected function _killFacebookCookies()
{
// get your api key
$apiKey = $this->getConfig()->getApiKey();
// get name of the cookie
$cookie = $this->getConfig()->getCookieName();
$cookies = array('user', 'session_key', 'expires', 'ss');
foreach ($cookies as $name)
{
setcookie($apiKey . '_' . $name, false, time() - 3600);
unset($_COOKIE[$apiKey . '_' . $name]);
}
setcookie($apiKey, false, time() - 3600);
unset($_COOKIE[$apiKey]);
}
回答by Dharti Patel
You can logout the facebook user and redirect the user to your website page with PHP code like this :
您可以注销 facebook 用户并将用户重定向到您的网站页面,使用 PHP 代码如下:
header("Location: " . $facebook->getLogoutUrl(array('next'=>"http://yourwebsite.com/redirectAfterFacebookLogout.php")));
header("位置:" . $facebook->getLogoutUrl(array('next'=>"http://yourwebsite.com/redirectAfterFacebookLogout.php")));
回答by Harsh Alkutkar
(NEW FACEBOOK SDK) For me the getconfig() wouldn't work.So I had to find the new functions from the base_facebook.php file and add this bit of code in it. Then call it in your calling file. Before you do , call $facebook->destroySession();
(新的 FACEBOOK SDK)对我来说 getconfig() 不起作用。所以我必须从 base_facebook.php 文件中找到新函数并在其中添加这段代码。然后在您的调用文件中调用它。在你这样做之前,调用 $facebook->destroySession();
public function _killFacebookCookies()
{
// get your api key
$apiKey = $this->getAppId();
// get name of the cookie
$cookie = $this->getSignedRequestCookieName();
$cookies = array('user', 'session_key', 'expires', 'ss');
foreach ($cookies as $name)
{
setcookie($apiKey . '_' . $name, false, time() - 3600);
unset($_COOKIE[$apiKey . '_' . $name]);
}
setcookie($apiKey, false, time() - 3600);
unset($_COOKIE[$apiKey]);
}

