Reputation: 39
I want to redirect users they are connected to facebook and not to anoying users they are not connected to facebook. How to do it? I am using this code for aquire data from facebook. This code is executed at first line on my page based on Wordpress .
this_code_php:
<?PHP
session_start();
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'my_number_here',
'secret' => 'my_number_here',
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
//echo '$logout '.$logoutUrl;
} else
{ $loginUrl = $facebook->getLoginUrl();
//echo '$login '.$loginUrl;
echo "<script type='text/javascript'>top.location.href = '$logoutUrl';</script>";
}
?>
There is the difference between CONNECTING to facebook and LOGGING to my application. Note, that if I know that user is connected to facebook (e.g. has open facebook tab) I want to automatically redirect him to my page with all facebook data. But I will not bother other users (not facebook registered) by facebook login page. If they are connected - OK - let simply redirect to my page with facebook data. If they are not - forget facebook login and forget facebook data – avoid facebook. Problem is line: echo "top.location.href = '$logoutUrl';";
What program does? If user is connected to facebook - program is redirecting next and user didn't know if any action was taken. But if user is not connected to facebook program displaying user login page, even for user they are not registered on facebook.
I want to replace line above by this line: if (/user is connected/) echo "top.location.href = '$logoutUrl';";
but I don't know how to check if user is currently connected to facebook? If it is possible in PHP or if I have to link PHP and Javascript, which is little difficult. And how to do it?
Upvotes: 3
Views: 4590
Reputation: 268
Another option is user use $facebook->getUser(). If not logged into Facebook this will return 0.
Upvotes: 1
Reputation: 906
CBroe is right. You have to check user status client-side. To add some details to what CBroe said, the document gives you useful example.
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your app
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
I wanted to post this as a comment to previous answer to support it, but comment form ignores line breaks...
Upvotes: 3
Reputation: 96315
For a user that has not already connected to your app, you get absolutely no info – not even whether they are currently logged in to Facebook or not.
Once the user has connected to your app, you can use FB.getLoginStatus
from the JavaScript SDK to determine whether they are currently logged in to Facebook or not.
This has to be done client-side, because only the JS SDK is able to make a cross-domain request to check for cookies set under the facebook.com domain – this is not possible server-side.
One you have determined their status client-side, the PHP SDK is able to pick up on this on the next request.
Upvotes: 0