FB API Cookie标头错误

时间:2012-12-17 08:29:52

标签: php cookies http-headers facebook-php-sdk

我有以下代码:

require '../lib/facebook-php-sdk/src/facebook.php';

$facebook = new Facebook(array(
'appId'  => '143944345745133',
'secret' => 'xxxxxxxx',
'cookie' => true,
));

$user = $facebook->getUser();

if ($user) {
   try {
      $user_profile = $facebook->api('/me');
   } catch (FacebookApiException $e) {
      $user = 0;
   }
} 
if ($user) { 
  setcookie('loggedUserId', $user, time()+36000);
  setcookie('loggedUserName', $user_profile['name'], time()+36000);
} else {
  setcookie('loggedUserId', '', time()-3600);
  setcookie('loggedUserName', '', time()-3600);
} 

我在php错误日志文件中收到错误:

[17-Dec-2012 06:11:34 UTC] There exists a cookie that we wanted to clear that we 
couldn't clear because headers was already sent. Make sure to do the 
first API call before outputing anything.

请帮我修理一下。结果用户体验是,在我登录后,需要重新加载页面才能实际登录。 感谢

1 个答案:

答案 0 :(得分:0)

代码中的某些内容在调用setcookie之前输出了一些内容。它可能是一个调试消息。甚至在打开<?php标签之前的空行。