开发者

Facebook Application Displays Nothing (PHP error)

I believe this is a PHP error but I'm not 100% sure. Basically, I am trying to build an app for my webzine, and have pulled through the static content, which worked fine. I then converted to PHP and tried to include the PHP Facebook SDK. I've used the correct App ID and Secret.

Here is the code I have used at the start of my index.php file.

<?php
    require 'fb/facebook.php';

    $fbconfig['appUrl'] = "http://apps.facebook.com/dailydischord/";

    // create instance of the Facebook application
    $facebook = new Facebook(array(
        'appId' => '<appid>',
        'secret' => '<appsecret>',
        'cookies' => 'true',
    ));

    // get the id of the user using the app
    $user = $facebook->getUser();

    if($user) {
   开发者_运维知识库     try {
            //if user is authentic, proceed
            $user_profile = $facebook->api('/me');
        }
        catch(FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }

    // if user is authentic, generate logout URL
    if($user) {
        $logoutUrl = $facebook->getLogoutUrl();
?>
<!DOCTYPE HTML>
<html>....

I then go on to do this after the content is finished.

<?php
    } else {
        $loginUrl = $facebook->getLoginUrl();
    }
?>

I did get an error, which I no longer get, which is weird. It was something like this.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started...

The URL for the app is here: http://apps.facebook.com/dailydischord/

Any help would be greatly appreciated. If I have missed anything, please just ask and I'll try provide some more insight.


You are trying to send (session) headers, while they are already sent.

Probably there is a newline before the <?php. If not, try to put session_start() at the beginning of your code (The first line after the <?php). If that doesn't work too, put ob_start() at the beginning of your code (It's dirty, but it will work).


You are trying to output headers, in this case sessions. Headers are the first thing processed by the browser. When you want to change this after you already output HTML, it will give you this error.

Best is too not change headers after already outputting data, also you can use the following line and put it and the top of your code:

ob_start();

Beware that using this, is not a proper fix, but it turns on the output buffering (allowing you to change headers when already putting HTML code).

The following is optional but is proper, using the ob_start() function and completely at the end of your output you have to turn off buffering and output what is in the buffer.

ob_end_flush();

For more information I would tell you to go to the PHP documentation page.


Since your output is completely blank, it implies either a syntax error or a failed require. Do you have access to the webserver error log? It would probably tell you the exact problem. For a quick check you can change the require to include to see if you then get any output (even if it's just onscreen error messages).


I managed to fix the issue myself. It appears the last bit of code was the problem (the "else" statement). This is the code I used.

<?php
    } else {
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri' => $fbconfig['appUrl']));
        print "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    }
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜