0

这个例子实际上是从 Facebook 复制的。

当我访问该页面时,行为很奇怪:“登录”首先似乎工作正常。然后它会显示“注销”链接。但是当我点击它时,我再次得到完全相同的屏幕,再次出现“注销”链接。刷新也进入同一屏幕。

但是,与 Facebook 核对,它确实让我退出了。

如果我关闭浏览器并重新打开它,“登录”现在会正确显示。

<?php

require 'facebook.php';

$facebook = new Facebook(array(
  'appId' => 'xxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if ($user) {
  $params = array( 'next' => 'http://xxxxxxxxxx' );
  $logoutUrl = $facebook->getLogoutUrl($params);
} else {
  $params = array( 'redirect_uri' => 'http://xxxxxxxxxx' );
  $loginUrl = $facebook->getLoginUrl($params);
}

?>

<!doctype html>
<html>
  <head></head>
  <body>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
    <?php endif ?>

    <?php if ($user): ?>
      Picture = <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
      User Object = <?php print_r($user_profile); ?>
    <?php else: ?>
      User is not Connected.
    <?php endif ?>

  </body>
</html>
4

2 回答 2

1
$past = time() - 3600;
foreach ( $_COOKIE as $key => $value )
{
    setcookie( $key, $value, $past, '/' );
}

你可以试试这段代码。它只会销毁从您的站点保存的所有 cookie。它为我工作..!

于 2013-06-10T19:03:58.063 回答
0

创建一个文件 logout.php

<?php 
session_start();            //start session
$_SESSION = array();    //clear session array
 session_destroy();      //destroy session
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Log Out</title>
</head>

<body>
<p>You have successfully logged out!</p>
<p>Return to the <a href="....index.php">Home</a> page</p>

</body>
</html>

并更改您检查用户状态的代码

if ($user) { 
    $params = array( 'next' => 'http://....../logout.php' );
    $logoutUrl = $facebook->getLogoutUrl($params);
} else {
  $loginUrl = $facebook->getLoginUrl();
}

使用 $logoutUrl 注销用户。

<?php if ($user): ?>
<?php echo "Welcome, ".$me['first_name']. " " .$me['last_name']   ." <br />";
      echo "Id: " . $me['id'] ." <br />";  ?>

<a href="<?php echo $logoutUrl; ?>">  Logout </a> <br />

<?php else: ?>
  <a href="<?php echo $loginUrl; ?>"> 

   <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"> </a>  
 <?php endif ?>

希望它能正常工作

于 2014-04-10T10:34:43.697 回答