tags:

views:

388

answers:

3

i am building a facebook app which is somehow generating these weird errors. the following are the errors I am facing:

Warning: Cannot modify header information - headers already sent by (output started at /home/amitver/public_html/roadies/index.php:2) in /home/amitver/public_html/roadies/facebook.php on line 395

Warning: Cannot modify header information - headers already sent by (output started at /home/amitver/public_html/roadies/index.php:2) in /home/amitver/public_html/roadies/facebook.php on line 395

Warning: Cannot modify header information - headers already sent by (output started at /home/amitver/public_html/roadies/index.php:2) in /home/amitver/public_html/roadies/facebook.php on line 395

Warning: Cannot modify header information - headers already sent by (output started at /home/amitver/public_html/roadies/index.php:2) in /home/amitver/public_html/roadies/facebook.php on line 399

the first few lines in index.php are:

<?php

require_once 'facebook.php';
//facebook init
    $appapikey = '...';
    $appsecret = '...';
    $facebook = new Facebook($appapikey, $appsecret);
    $user_id = $facebook->require_login();


    $userInfo = $facebook->api_client->users_getInfo($user_id, array('name'));
    $name = $userInfo[0]['name'];

    //$useruidname = $facebook->api_client->fql_query("SELECT name FROM user WHERE uid='$user_id'");

    // Greet the currently logged-in user!
    echo 'Hello, '.$name;   

?>

the lines 394-400 in facebook.php are:

foreach ($cookies as $name => $val) {
  setcookie($this->api_key . '_' . $name, $val, (int)$expires, '', $this->base_domain);
  $_COOKIE[$this->api_key . '_' . $name] = $val;
}
$sig = self::generate_sig($cookies, $this->secret);
setcookie($this->api_key, $sig, (int)$expires, '', $this->base_domain);
$_COOKIE[$this->api_key] = $sig;

can someone help me solve this?

A: 

Check for white space in any of the files that you are including. Just a single " " and PHP will send that output to the user agent (browser) and you can no longer do header things like change cookies or send header() data.

That error states that index.php line 2 (the require) is causing something to be sent. So check to see if anything is output in that include or that the file includes blank lines/spaces at the end of the facebook.php file.

Xeoncross
+2  A: 

theres probably some newlines at the top of index.php that are getting outputted before the setcookie call.

jspcal
you were right. there was a blank line at the top of <?php.i thought python was difficult. really cant believe i spent 2 hours on this...thanks a lot.
amit
A: 

About the weird error, it's pretty straight forward. It says you've already started sending output, so you can't send custom headers. When you send output to the client, PHP has to send headers first. Once headers have been sent, you can't change them. It looks like you're either sending whitespace or echo'ing data or sending the Set-Cookie header prematurely. You should build your headers into a list or string before sending them, and generally you would do the same with your content. One way to delay sending output until after all headers have been sent is to capture the output stream. Like so:

ob_start();

echo 'Hello world';

$output = ob_get_contents();

ob_end_clean();

Sure, your problem was infact a blank line, but doing that you wouldn't have this problem.

Eric Muyser