I want to get current loggedin facebook user in php sdk. Here is what i have tried:
$facebook = new \Facebook(array(
'appId' => 'XXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXX',
));
$user = $facebook->getUser();
var_dump($user);
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
echo $logoutUrl;
} else {
$loginUrl = $facebook->getLoginUrl();
echo $loginUrl;
}
When I load the page with above code, I get the loginUrl even though I am logged in facebook. Only when i load $loginUrl in the browser, I am able to get facebook user.
How do I get the current user at first load of this page?