Jump to content

Facebook PHP-SDK - login/logout problems


pushkidman

Recommended Posts

Hi! I am trying to put Facebook SDK to work.

I had this code, which I slighly modified, from some internet tutorial:

<?php
$fbconfig['appid'] = "...";
$fbconfig['secret'] = "...";
$fbconfig['baseurl'] = "...";

$user = null; //facebook user uid
try {
    include_once "facebook.php";
} catch (Exception $o) {
    error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
            'appId' => $fbconfig['appid'],
            'secret' => $fbconfig['secret'],
        ));

//print $facebook->getAccessToken() . '<br/>';

$user = $facebook->getUser();

$loginUrl = $facebook->getLoginUrl(
        array(
            'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
            'redirect_uri' => $fbconfig['baseurl']
        )
);

if ($user) {
    if (isset($_GET['publish'])) {
        try {
            $publishStream = $facebook->api("/$user/feed", 'post', array(
                'message' => "...",
                'link' => '...',
                'picture' => '...',
                'name' => 'iPlay, iWin',
                'description' => '...'
                    )
            );
            //as $_GET['publish'] is set so remove it by redirecting user to the base url 
        } catch (FacebookApiException $e) {
            d($e);
        }
        $redirectUrl = $fbconfig['baseurl'] . '?success=1';
        header("Location: $redirectUrl");
    }

    //fql query example using legacy method call and passing parameter
    try {
        $fql = "select name, hometown_location, sex, pic, pic_square, pic_big from user where uid=" . $user;
        $param = array(
            'method' => 'fql.query',
            'query' => $fql,
            'callback' => ''
        );
        $fqlResult = $facebook->api($param);
        
        print_r($fqlResult);
        
        $fql = "select url, real_height from profile_pic where id=" . $user;
        $param = array(
            'method' => 'fql.query',
            'query' => $fql,
            'callback' => ''
        );
        $fqlResult = $facebook->api($param);
    } catch (Exception $o) {
        d($o);
    }
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>FB Test</title>

    </head>
    <body>

   <?php if (!$user) { ?>
            <a href="<?= $loginUrl ?>">Facebook Login</a>
        <?php } else { ?>
            <div><a href="<?= $facebook->getLogoutUrl(); ?>">Facebook Logout</a></div>
            <div>You are in! <a href="?publish=1">Share your details with us</a>!</div>
        <?php } ?>

        <?php if ($_GET['success']) : ?>
            <div>Check you FB wall!</div>
        <?php endif; ?>
    </body>
</html>

There are two troubles I have with this code:

 

1. When I am logged in with Facebook and I go to this page, I still have to connect to FB manually via $loginUrl. Is this OK in terms of SDK? Or am I doing something wrong?

 

2. When I click on logout URL, nothing happens on the page. It seems to log me out properly out of my actual FB account, but on the page $user object still exists. Sometimes I have to click logout URL several times, before I actually see changes on the page. Why is that?

 

Thanks in advance for any sort of help!

Edited by pushkidman
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.