facebook php sdk automatically logs out user - facebook

I am trying to integrate facebook login in my website.
I am facing a weird problem wherein the user keeps getting logged out almost every 10 minutes from my website, although he remains logged in to facebook:
I don't have much clue about the cause of the error, but this might help. I had some error in my javascript code and since during that time, javascript was not being executed, I somehow remained logged in.
For implementing facebook login i am using geloginUrl and getLogoutUrl functions in php, I have added some code to post on friends' wall etc in javascript as well.
That is all the relevant information according to me.
Let me know, if any additional details might be required to answer this question correctly.
This is what my code looks like:
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = 0;
}
}
if ($user) {
setcookie('uid', $user, time()+36000, '/', 'mywebsite.com');
setcookie('uname', $user_profile['name'], time()+36000, '/', 'mywebsite.com');
} else {
setcookie('uid', $user, time()+36000, '/', 'mywebsite.com');
setcookie('uname', $user_profile['name'], time()+36000, '/', 'mywebsite.com');
}
$loginUrlParams = array(
'scope' => 'email,user_activities,friends_activities,friends_interests,user_interests',
'redirect_uri' => 'http://mywebsite.com/getfbToken.php'
);
$loginUrl = "";
$logoutUrl = "";
$logoutUrlParams = array( 'next' => 'http://mywebsite.com/logout.php' );
// getting login url from facebook if user is invalid
if (!$user)
$loginUrl = $facebook->getLoginUrl($loginUrlParams);
else
$logoutUrl = $facebook->getLogoutUrl($logoutUrlParams);

Related

"facebook.com" missing from login page URL

I can not login with facebook from any applications, including from mine. This is caused by the lack of "facebook.com" in login page URL. When we click the login button, the URL should be
https://www.facebook.com/login.php?skip_api_login=1&api_key=XXX....
but in my case :
https://www./login.php?skip_api_login=1&api_key=XXX.. (without "facebook.com")
So, the web page can not display anything. It just happened today, everything was normal yesterday. I have try facebook basic example code and the problem still same. Here's the code :
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '137737616434442',
'secret' => 'XXX',
'baseurl' => 'http://localhost/facebookconnect/login/example.php',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'http://localhost/facebookconnect/login/example.php' ));
session_destroy();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,user_birthday,friends_birthday,publish_stream,user_location,friends_location,user_work_history,user_likes,friends_work_history,user_about_me,friends_about_me,user_hometown',
'redirect_uri' => 'http://localhost/facebookconnect/login/example.php'));
}
$naitik = $facebook->api('/naitik');
?>
Any help would be greatly appreciated, thank you
I think it's Facebook app problem. Looks like the problem occurs everywhere.
I restart my connection and, everything is back to normal. Looks weird, but it works.
My Facebook page is missing today (August 28, 2013) too! It was there at midnight last night but won't load today! My Outlook/Hotmail won't work either. Loads but won't open anything. I think it's been hijacked after a friend's hotmail was hijacked. This computer is going to the hospital tomorrow. Until then it is being unplugged from the internet.

unable to to open redirected page after facebook authentication

i am using facebook connect under codeigniter.after authentication i want to redirect on success method of my controller
here is my controller:
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Facebook_model');
}
function index()
{
$fb_data = $this->session->userdata('fb_data');
$data = array(
'fb_data' => $fb_data,
);
$this->load->view('welcome', $data);
}
function topsecret()
{
$fb_data = $this->session->userdata('fb_data');
if((!$fb_data['uid']) or (!$fb_data['me']))
{
redirect('welcome');
}
else
{
$data = array(
'fb_data' => $fb_data,
);
$this->load->view('topsecret', $data);
}
}
function success()
{
$this->load->view('welcome_message');
}
}
my model for facebook api access:
class Facebook_model extends CI_Model {
public function __construct()
{
parent::__construct();
$config = array(
'appId' => '261066574000678',
'secret' => ' 79e11f65449988965362f58e9a4aabd7',
'fileUpload' => true, // Indicates if the CURL based # syntax for file uploads is enabled.
);
$this->load->library('Facebook', $config);
$user = $this->facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
$profile = null;
if($user)
{
try {
// Proceed knowing you have a logged in user who's authenticated.
$profile = $this->facebook->api('/me?fields=id,name,link,email');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$fb_data = array(
'me' => $profile,
'uid' => $user,
'loginUrl' => $this->facebook->getLoginUrl(
array(
'scope' => 'email,user_birthday,publish_stream', // app permissions
'redirect_uri' => 'https://sanjay.localhost.com/index.php/welcome/success' // URL where you want to redirect your users after a successful login
)
),
'logoutUrl' => $this->facebook->getLogoutUrl(),
);
$this->session->set_userdata('fb_data', $fb_data);
}
}
since i am testing this on localhost host,i also edited my host file and changed my localhost hostname to sanjay.localhost.com.redirect happens but not happens..i think may be because of querystring.when redirects happens redirect uri is
=">https://sanjay.localhost.com/index.php/welcome/success?state=ff5712299510defa&code=AQCaD-FAd1shuW#=
i am not understanding how to handle state and code inside of query string.
please help.
Thank you for contacting me on my blog. First of all, Facebook is discontinued the localhost support. Her is the link https://developers.facebook.com/bugs/128794873890320.
I have not developed any app using codeigniter, I use CakePHP but the auth follow should be same.
1. Create a fb_login function in user controller.
2. This function will follow this logic.
a. Use $facebook->getUser() to get user id.
b. Then use $facebook->api('/me') to be sure.
3.If you get FacebookApiException then send user to login with Facebook. If you use official SDK then the current url will be added to redirect url.
4.the Facebook will redirect your user after sign in. so you will get data using $facebook->getUser(). Save this data in session for further use in you app. then redirect user to you control page or any other page. CakePHP has setFlash() function wich show what ever msg set in the control panel in view. I think Codeignator should have some thing like this. If not you can simply set a msg in session and redirect user. Then unset the msg after showing the msg.
Here is full code
$uid = $facebook->getUser();
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//echo $e->getMessage();
$uid = null;
}
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream,offline_access,email'
),''
);
if (!$uid) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
//search using uid on your user table to check if the use is returnign user or new user.
if($new_user ==1)
{
$this->Session->setFlash('please sign up');
//redirect to sign up
}
else
{
$this->Session->setFlash('you are good lad');
//reditect to control panel
}

Facebook Connect works offline, but not online "This webpage has a redirect loop"

Access to my developed website is done using facebook connect.
Everything is OK offline, but not online. I get a navigation error (using Chrome) "This webpage has a redirect loop" :
The webpage at https://www.facebook.com/dialog/oauth?client_id=209633612480053&redirect_uri=http%3A%2F%2Fwww.bluward.com%2Faccess%2Flogin_facebook&state=299262ddf89afbf382452df89c9a2ce8&scope=email%2C+user_birthday%2C+user_about_me%2C+user_location%2C+publish_stream&fbconnect=1#= has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
Here is the PHP code (using CodeIgniter framework):
public function login()
{
// Get the FB UID of the currently logged in user
$uid = $fb->getUser();
// If the user has already allowed the application, you'll be able to get his/her FB UID
if($uid) {
try {
$profile = $fb->api(array(
'method' => 'users.getinfo',
'uids' => $uid,
'fields' => 'uid, first_name, last_name, pic_square, pic_big, sex, birthday_date, current_location, email'
));
// Only the first user.
$profile = $profile[0];
} catch (FacebookApiException $e) {
return false;
}
// Do stuff when already logged in.
return $profile;
} else {
// If not, let's redirect to the ALLOW page so we can get access
redirect($this->getLoginUrl(array(
'scope' => 'email, user_birthday, user_about_me, user_location, publish_stream',
'fbconnect' => 1
)));
}
}
And depending on the profile info, I check if the user already exists on database, then log him/her, and if not exists on database, signup him/her.
Update:
When I clear Facebook cookies, the redirect to login page is successful, and I'm able to fill email/password.
Unfortunately, when I click "log in" button, I get the same error message as I displayed above.
It looks like your using and older version? Here is how i would handle issue with php-sdk 3.1.1 "the most current version".
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$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();
} else {
$params = array(
scope => 'read_stream,publish_stream,publish_actions,read_friendlists',
//redirect_uri => $url
);
$loginUrl = $facebook->getLoginUrl($params);
echo '<script> top.location.href=\''.$loginUrl.'\'</script>';
};

Facebook application - server not responding

After the FBML problems I switched to iFrame apps. All well for now except that 2 things:
1) if I did not entered in the app for a few hours and I try to enter, sometimes it gives me the error page that my server did not responded. It shows the Faceboob page with iFrame inside showing the error. After 1 or 2 refreshes it's working fine.
2) I implemented the new SDK with login like this
$facebook = new Facebook(array(
'appId' => $fbID,
'secret' => $fbSecret,
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
d($e);
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>
top.location.href = '$loginUrl';
</script>";
exit;
}
The problem is my first redirect, when user enters for the first time. It redirects to my server URL not my Facebook app URL. If I try to put it like this:
$loginUrl = $facebook->getLoginUrl(
array(
'redirect_uri' => 'http://apps.facebook.com/xxx/'
)
);
the login is entering in an infinite loop.
Any help solving this two problems is welcome:-)
Thanks
Darko4spain when the user is redirected back to your site, you can detect if is in iframe with javascript and send them back. I have same issue with my app. This is not a pretty fix but it works.
if (window!=window.top) {
//FB.Canvas.setAutoResize();
setTimeout("FB.Canvas.setAutoGrow()", 1400);
}else {
top.location.href = 'https://apps.facebook.com/AnotherFeed/?ref=redirect#comments';
}
example: https://anotherfeed.com/plugins/index.php will redirect you back to my canvas if you are not in the canvas.

Facebook login to site bugs

When users come to my site they have the option to log in through facebook. If they are logged in to facebook, and hit the fb login button, it will show all the appropriate logged in dialog. However, if you log out of facebook (either through my site or through facebook itself) and then try to log in through my site again, it will bring you to facebook to sign in, then bring you back to my site. The url changes from x.net/index.php to x.net/index.php?state=... bunch of letters / numbers, but then doesn't return logged in dialog. Hit the button once again after that, and the page refreshes with a different (but similar) url, and returns all the logged in dialog.
Any ideas on how to fix this?
Here is the code I use to connect to facebook and get the login and logout urls.
//facebook application
$fbconfig['appid' ] = "myAppId";
$fbconfig['secret'] = "myAppSecret";
$fbconfig['baseurl'] = "http://myNeatSite.net/index.php";
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
$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'],
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream',
'redirect_uri' => $fbconfig['baseurl']
)
);
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
As far as I see, you only process the case $user is not null (which means the user is already logged in), but ignore the other case.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}