kontroler index (framework Kohana 3):
public function action_index()
{
$this->session->destroy();
$facebook = Facebook::getInstance();
$session = $facebook->getSession();
$uid = NULL;
$me = NULL;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
$me = NULL;
}
}
$this->model_facebook->insert_user($me);
$user_chosen_gender = $this->model_facebook->user_chosen_gender($me['id']);
$user = $this->model_facebook->get_user_by_facebook_id($me['id']);
} else {
$user_chosen_gender = FALSE;
$user = FALSE;
}
if ($user_chosen_gender) {
$this->request->redirect('/action/statistics');
}
$content = View::factory('index/index')
->set('uid', $uid)
->set('me', $me)
->set('user_chosen_gender', $user_chosen_gender)
->set('user', $user);
$this->template->content = $content;
$this->template->set_global('facebook', $facebook);
$this->template->set_global('session', $session);
}
widok:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US"> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="P3P" href="http://topapp.nanigans.com/p3p.xml#policy" content='CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"' /> <?php echo html::style('media/css/reset.css') ?>
<?php echo html::style('media/css/main.css') ?>
<?php echo html::style('media/css/jScrollPane.css') ?>
<?php echo html::script('media/js/custom-form-elements.js') ?>
<?php echo html::script('media/js/jquery-1.4.2.min.js') ?>
<?php echo html::script('media/js/jScrollPane.js') ?>
<?php echo html::script('media/js/jquery.mousewheel.js') ?>
<?php echo html::script('media/js/scripts.js') ?>
<script type="text/javascript"> window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
<?php if (strtolower(json_encode($session)) == 'null'): ?>
top.location.href = "<?php echo $facebook->getLoginUrl(array('req_perms' => 'publish_stream', 'next' => 'http://apps.facebook.com/' . Kohana::config('facebook.appName') . '/', 'canvas' => 1, 'fbconnect' => 0)) ?>";
<?php endif; ?>
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
<?php echo @$content ?>
rezultat:
w logach informacja "2010-09-20 16:31:35 --- ERROR: FacebookApiException [ 0 ]: An active access token must be used to query information about the current user. ~ APPPATH/classes/facebook.php [ 509 ]", na stronie aplikacji petla przekierowan (sesja == 'null')