会议正在消失

时间:2012-08-27 11:22:54

标签: php session cookies twitter oauth

我正在使用Abraham William的Twitter oAuth库。

以下是我的代码:

if (isset($_REQUEST['oauth_token'])){
  print_r($_SESSION);
  exit;
}
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);     
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
  case 200:
    /* Build authorize URL and redirect user to Twitter. */
    $url = $connection->getAuthorizeURL($token);
    print "<script>self.location='$url';</script>";
    break;
  default:
    /* Show notification if something went wrong. */
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
    return;
}

如果我在重定向之前检查$ _SESSION,那么$ _SESSION就在那里。发生重定向时,设置$ _REQUEST ['oauth_token'](我验证了这一点),但$ _SESSION不再存在。

我的浏览器设置为接受Cookie和第三方Cookie。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你没有session_start();在页面顶部。在您的代码使用任何会话变量之前,需要session_start()。