I am getting the following error in my application that asks the user to login using facebook account : Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/show.php:4) in XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/lib/facebook.php on line 37
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/show.php:4) in XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/lib/facebook.php on line 37
I don't know why this error is shown in spite of the face that it does not appear when I was hosting my app on phpcloud.com
Here is the code of the show.php
<?php
$id=$_GET['id'];
include('lib/db.php');
require 'lib/facebook.php';
require 'lib/fbconfig.php';
// Connection...
$user = $facebook->getUser();
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl();
try {
$userdata = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$_SESSION['facebook']=$_SESSION;
$_SESSION['userdata'] = $userdata;
$_SESSION['logout'] = $logoutUrl;
header("Location: home.php?id=$id");
}
else
{
$loginUrl = $facebook->getLoginUrl(array( 'scope' => 'user_about_me,user_activities,user_birthday,user_checkins,user_education_history,user_events,user_groups,user_hometown,user_interests,user_likes,user_location,user_notes,user_online_presence,user_photo_video_tags,user_photos,user_relationships,user_relationship_details,user_religion_politics,user_status,user_videos,user_website,user_work_history,email,read_friendlists,read_insights,read_mailbox,read_requests,read_stream,xmpp_login,ads_management,create_event,manage_friendlists,manage_notifications,offline_access,publish_checkins,publish_stream,rsvp_event,sms,publish_actions,manage_pages'
));
echo '<div class="style1"><a target="_top" href="'.$loginUrl.'"><img src="facebook.png" title="Login with Facebook" /></a></div>';
}
?>
Here is the facebook.php code :
<?php
require_once "base_facebook.php";
class Facebook extends BaseFacebook
{
public function __construct($config) {
if (!session_id()) {
session_start();
}
parent::__construct($config);
}
protected static $kSupportedKeys =
array('state', 'code', 'access_token', 'user_id');
protected function setPersistentData($key, $value) {
if (!in_array($key, self::$kSupportedKeys)) {
self::errorLog('Unsupported key passed to setPersistentData.');
return;
}
$session_var_name = $this->constructSessionVariableName($key);
$_SESSION[$session_var_name] = $value;
}
protected function getPersistentData($key, $default = false) {
if (!in_array($key, self::$kSupportedKeys)) {
self::errorLog('Unsupported key passed to getPersistentData.');
return $default;
}
$session_var_name = $this->constructSessionVariableName($key);
return isset($_SESSION[$session_var_name]) ?
$_SESSION[$session_var_name] : $default;
}
protected function clearPersistentData($key) {
if (!in_array($key, self::$kSupportedKeys)) {
self::errorLog('Unsupported key passed to clearPersistentData.');
return;
}
$session_var_name = $this->constructSessionVariableName($key);
unset($_SESSION[$session_var_name]);
}
protected function clearAllPersistentData() {
foreach (self::$kSupportedKeys as $key) {
$this->clearPersistentData($key);
}
}
protected function constructSessionVariableName($key) {
return implode('_', array('fb',
$this->getAppId(),
$key));
}
}
?>
Now here is the fbconfig.php
<?php
//Facebook Application Configuration.
$facebook_appid='XXXXXXXXXXXXXXXXXXX';
$facebook_app_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$facebook = new Facebook(array(
'appId' => $facebook_appid,
'secret' => $facebook_app_secret,
));
?>
I have found a leading thread in this post (What is Output Buffering?) that states that outputbuffering is a solution to such problems. If so, where shall I add it ? in show.php or facebook.php and how to add it ?