I am using the following code and it's v strange - when i log out, if i try to actually go back into facebook i cannot but my code below does not register that i'm logged out and will show my facebook user id still
include '/home/php_/lib/fb_libs/facebook.php';
$facebook = new Facebook(array(
'appId' => '123456789',
'secret' => 'abcdefghilk',
'cookie' => true
));
$user = $facebook->getUser();
echo "user id is $user";
if ($user){
try { $user_profile = $facebook->api('/me'); }
catch (FacebookApiException $e) { echo $e->getMessage(); }
}
if ($user)
{
$logoutURL = $facebook->getLogoutURL();
echo "<a href='$logoutURL'>Log out </a>";
}
else
{
$loginURL = $facebook->getLoginURL(array(
'scope' => 'publish_stream,read_friendslists'
));
echo "<a href='$loginURL'>Log in </a>";
}
I understand there are other methods e.g. FB.getLoginStatus() so this is not a duplicate question but why, when this is on github facebook SDK, does it not work?
I think your cookie is causing the problem try not setting the cookie.
Related
My PHP Facebook SDK is not working and always returns $user=0 Why?
It is my first time to deal with facebook sdk
my code:
require './facebook-php/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx'
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if($user) {
$logoutUrl = $facebook->getLogoutUrl(); echo 'there is a user';
} else {
$loginUrl = $facebook->getLoginUrl();
echo 'there is noo user <a href='.$loginUrl.'>login</a>';
}
below is the url return code :
http://localhost/e-commerce/testit.php?code=AQA8DckDMqNarptbCUIfAmn2tIyn1mjVBmHTjOMaQ4GqsFW_6vqemGW35TC07Kxq7kuB4QkKibHJ2-m9GntIx0biWEiR_FUuozsONvgc_tNmtGURpcm78DnIE26vT6hbjz10eZECuh-GLec9tGplhLrDNbLO-aX2Hv2Mu-8CQhAdiFQ6AYW3Nt09a8Rzahbn35Jz0cNyB_G7Ksjorq3iSs6Bpa6Tk0d7Vx0A0f1Mg77PgD6IuN_grjkNHDWmb_ywoqW48IqoxJUhnRmL3qi1G-CT7ft_f77rRr-VP-yQ-LTkdNja0rBzoFnFGccMuQ86u8tk7nfnx5qrSLBpHLEIBqQT&state=84ff9f36e8de53d13c96bcf7cb2da3b3#=
I have uploaded Facebook setting image
Facebook settings images
I am trying to get an access token using the latest Facebook SDK for PHP. To my knowledge I am doing everything correct, but when it redirects to facebook.com/oauth/dialog, it just has a white screen. My code:
<div class="authorize_btn" style="float:right; margin-top:-35px; padding-right:10px">
<?php
include ('facebook/facebook.php');
$facebook = new Facebook(array( 'appId' => 'xxxxx',
'secret' => 'xxxx',
'cookie' => true ));
$session = $facebook->getUser();
$me = null;
if ($session) {
try {
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
if ($me) {
$logout = $facebook->getLogoutUrl();
echo "<button class='btn btn-primary'></button";
} else {
$login = $facebook->getLoginUrl();
echo "<a href='$login'>Authorize</a>";
}
?>
</div>
Get the latest Facebook SDK for PHP - the one you are using is about 2 years old and not supported .
https://developers.facebook.com/docs/php/gettingstarted/4.0.0
I'm using the following code as part of my facebook application that get user's permissions (mainly : likes ). This code will detect if the user is a fan of a page or not and based on that it will show some content or redirect him to another page, if he is not a fan. The main problem is that it works only one time or two times and then it shows nothing.
http://pastebin.com/RxefpmzD
<?php
require 'lib/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/333605930105938");
if( !empty($likes['data']) )
echo "<iframe width=\"745\" height=\"535\" src=\"http://www.youtube.com/embed/".$donnees['link']."?fs=1&autoplay=1&loop=1\" frameborder=\"0\" allowFullScreen=\"\"></iframe>";
else
echo "<iframe width=\"745\" height=\"535\" src=\"scren.php?id=".$donnees['id']."\" frameborder=\"0\" allowFullScreen=\"\"></iframe>";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
?>
When user presses the logout button, it doesn't clear the session or cookies. That means after I refresh it, it's still logged in. After I added destroy session it works, but it also logs out my facebook on www.facebook.com together with my web site.
<?php
function facebooklogin(){
include ('libs/facebook.php');
$facebook = new Facebook(array(
'appId' => '[my app id]',
'secret' => '[my secret]',
'cookie' => true
));
$session = $facebook->getUser();
$me = null;
if ($session)
{
try
{
$me = $facebook ->api('/me');
$facebook->api ('/me');
$_SESSION['id'] = $me['id'];
$_SESSION['email'] = $me['email'];
$_SESSION['name'] = $me['name'];
//print_r($me);
printf($me['id']);
printf($me['email']);
printf($me['name']);
}
catch (FacebookApiException $e)
{
echo $e->getMessage();
}
}
if ($me)
{
$logoutUrl = $facebook->getLogoutUrl();
echo "<a href='$logoutUrl'><img src=assests/images/fblogoutbtn.png></a>";
}
else
{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, read_friendlists,email')
);
echo "<a href='$loginUrl'><img src=assests/images/fbbtn.png></a>";
}
}
require_once($_SERVER['DOCUMENT_ROOT'] . '/facebook-php-sdk/facebook.php');
$facebook = new Facebook(array(
'appId' => FB_MAIN_APP_API_ID,
'secret' => FB_MAIN_APP_SECRET
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Above is the code I'm using to get the Facebook details for my application. In my localhost this is working fine and it gives my Facebook email, user id. After I upload this in to live server this function is not working and it's not giving my Facebook details. I checked on the Facebook app id and it is the correct one.
I cannot figure out why it's not working in live site since it is working in my localhost. Please help me with this. Thank you.
I think problem is in your app which your are using on server. So try the same app which you are using for your localhost and in login and logout url instead of localhost use $_SERVER['HTTP_HOST'].
Example :
$session->fb_logout = $facebook->getLogoutUrl(array('next' => "http://" . $_SERVER['HTTP_HOST'] . "/path/to/logout"));
First check that what $user_profile = $facebook->api('/me'); showing. Just print_r the variable $user_profile.
Ex :
$user_profile = $facebook->api('/me');
print_r($user_profile);
exit;
if it showing blank then surely it will go in catch block. Then write following in catch block.
print_r($e);
exit;
For geting the user id, user need to grant permission
$user_id = $facebook->getUser();
echo $uid;
if($user_id){
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user_id = null;
}
}else{
$login_url = $facebook->getLoginUrl();
echo("<br>login url=".$login_url);
};
After the user grant permission check the login_url, you may access user id.
Try this
Have you checked you have the correct URL in the configuration of your app in Facebook?
I have this piece of code to my FB apps and work properly, could you try it?
$session = $this->facebook->getUser();
if ($session) {
$logoutUrl = $this->facebook->getLogoutUrl();
try {
$data['uid'] = $this->facebook->getUser();
$me = $this->facebook->api($session);
$data['me'] = $me;
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_about_me, email',
'redirect_uri' => 'YOUR URL',
'canvas' => 1,
'fbconnect' => 0,
'display' => 'page',
));
}
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$this->load->view('MY VIEW', $data);