problem with facebook api - Keeps redirecting - php

I created an application with a name and connect URL on facebook.
I put below code in test.php here (test.php):
include_once 'facebook.php';
$fb = new Facebook('api_key', 'secret');
$user = $fb->require_login();
echo $fb->api_client->user;
echo $fb->api_client->session_key;
when I go to that page, it takes me to facebook login, after I login it redirects me to same test.php with an auth_token. But it doesn't stop there. For some reason, the script is taking me to facebook again and since I already logged in facebook redirecting me to test.php with another auth_token just like below
/test.php?auth_token=76a6eb21b872cdfe787ea85a240905dd&auth_token=597d7532bf53c8ce37cc003bcf7d2905
It never stops there, test.php taking me to facebook again and again :(
Please tell me what wrong??
Thanks a lot

adda line:
$facebook->require_frame();

Related

Facebook Login PHP: Something weird with my logout

(Sorry if I have redaction and spelling troubles)
Hi guys, I'm new on Facebook SDK and I have a huge problem (I need to solve it before today).
I'm trying to implement a facebook login in this site:
http://www.woonki.com/usuarios/index.php (Site Lang. is Spanish)
So, I downloaded a code from internet and I implemented that on my site. When the user makes the log-in, it works well!; But when the user makes the log-out, it's still marking the user session (even if the user has logged out of facebook)! In other words, my log-out script doesn't works :(
This is the "fb_logout.php" code
<?php
/* -----------------------------------------------------------------------------------------
IdiotMinds - http://idiotminds.com
-----------------------------------------------------------------------------------------
*/
require '../Connections/fb_config.php';
if((isset($_SESSION['User']) && !empty($_SESSION['User'])))
{
$_SESSION['fb_'.$appID.'_user_id'] = '';
$_SESSION['fb_'.$appID.'_access_token'] = '';
session_destroy();
header('Location: '.$base_url);
}
?>
So, I tried out my code and the 'if' condition isn't working! I don't know what is happening here, if my SESSION is not a SESSION or vice-versa.

Logout redirection

Hi im new to the facebook api and have managed to get a user to login using facebook, however I am trying to make a logout that redirects to a different page eg. logout.php The issue is when it logs out it redirects to the same page.
The code im using to get the logout link is:
$logoutUrl = $facebook->getLogoutUrl();
How can i make it so the logout url automatically logs you out of facebook and redirects you to logout.php?
Thanks in advance!
UPDATE
An example of the url i currently get from the getLogoutUrl() function:
https://www.facebook.com/logout.php?next=http%3A%2F%2FMYDOMAINHERE.com%2F&access_token=MYACCESSTOKENHERE
My guess is that if i can pass it a redirection URL to autogenerate with that function it would look something like this:
https://www.facebook.com/logout.php?next=http%3A%2F%2FMYDOMAINHERE.com%2FLOGOUT.PHP%2F&access_token=MYACCESSTOKENHERE
UPDATE
Never mind i just figured it out. Works the same way the login does with the
getLogoutUrl(array(
'next' => 'http://' . $_SERVER['HTTP_HOST'] . '/logout.php'
));
Do you mean this?:
header ("Location: $logoutUrl")
exit;

PHP SDK + Code Igniter

First, I downloaded the Facebook PHP SDK and applied the first answer in here:
Using Facebook PHP-SDK 3.x to register/login user with Codeigniter 2.1.0
In my controller:
I try to get the user: $this->facebook->getUser(); .. If it's 0, I call another function $this->facebook_login(); - else I save some data in the database like name for example.
In facebook_login(), a Login Url is generated: $this->facebook->getLoginUrl() - and is passed to the view, which displays it.
So far I click on the login url and it redirects to Facebook, asks for approval and authentication and so on - then redirects me back to the page in "1".
The problem is, getUser is returning zero. I printed the $SESSION variable and found out that only fb{appId}_state is being set.
I tried creating another php file (login.php) that is not in the CI folder with the following code:
<?php
define('BASEPATH', '');
require_once('application/libraries/facebook.php');
$fb = new Facebook(array('appId'=>'...', 'secret'=>'...'));
$user = $fb->getUser();
if($user == 0){
$login = $fb->getLoginUrl();
echo 'Login';
} else {
print_r($fb->api('/me'));
}
?>
If I visit login.php then visit the page in "1" above - getUser returns the user id and $_SESSION contains the user details.Therefore, I guess the problem is with CodeIgniter but I am not sure how to solve it.
I searched and tried several solutions but that's the best I could do. If possible, I don't want to use the JS SDK.
Thank you
I found a solution and wrote a blog post about it here.

facebook connect does not clear session cookie on logout

I'm doing some integration with Facebook on a project (using graph api) and everything was working fine until now I discovered that facebook doesn't clear my session when I click on the logout url. I'm doing logout through php, not javascript, so the logout url looks like:
https://www.facebook.com/logout.php?next=url&access_token=token
After clicking on that link the user is logged out on facebook, but the session still exists on my website. To actually clear the session I have to refresh the page one more time after clicking that url. This is a strange behavior, in my opinion.
What you guys think of this? I tried even to personally remove the facebook cookie, but it is still there, and it is cleared only after I hit the refresh button on my browser.
p.s. to get the facebook session I do something like this:
My_Facebook_Helper::instance()->getSession(); //it should be === null if it doesn't exist
p.p.s. it could be a bug? i don't remember having this issue about a week ago when I first started to implement this
I ran into this bug and realized it wasn't that the session wasn't being cleared, but if you have offline access, it will automatically get you a new session.
Try finding the formatData function somewhere at LoginWindow (AS3) and find this line:
vars.redirect_uri = FacebookURLDefaults.LOGIN_SUCCESS_URL
Change the value for 'http://www.facebook.com/' and logout from that html page when logged in.
This is a temporary solution to logout if you are developer, not the end user.
This is registered as a bug. Please add your own repro to this bug to help get it fixed
http://developers.facebook.com/bugs/250825644953332
I have just a solved a simular problem today.
Try using this to get your session:
$session = $facebook->getSession();
And this could also help for the logout url:
$logoutUrl = $facebook->getLogoutUrl(array('next' => $url, 'session_key' => $session['session_key']));
Hope it helps!
I tried this
$logoutUrl = $facebook->getLogoutUrl(array('next' => 'some url', 'session_key' => $session_key)) . 'session_key=null';

PHP: how to retrieve profile data from Facebook

I've been going through FB docs but I think I am totally lost now. My task is quite simple: I need to have an "import" link, user clicks it and receives FB popup where he authorizes the site, user is redirected back to my site, I access user's FB profile and retrieve some data. Also: I need to access and retrieve profile in PHP.
The first part goes well. I created mysite.com?page=import link which opens in popup and then redirects to https://graph.facebook.com/oauth/authorize?...
User then allows access and the popup is redirected back to mysite.com?...#access_token=...&expires_in=4031
Then I am going to close the popup and instead refresh the parent window that opened this popup by redirecting it to something like this mysite.com?page=register&access_token=...&expires_in=4031
Then I was going to use their PHP SDK hoping that it can take this access token and allow me to get user's data. However I have no luck so far. I've tried lots of things and went through facebook.php but can't see a solution. Please let me know how to do this: authorize user in a popup and then gather the profile data in php.
here is your working example.
it has a redirect and works entirely in php, i didnt do javascript beacuse this is easier and faster to write.
the main difference is that the returned code after the authorization page is only a code which allows you to fetch the actual access token incombination with your client secret.
otherwise anyone could get an access token for yuor application or you would have to pass your secret token in the url.
thats the reason for the second step.
in javascript we dont need that because facebook only redirects back to a whitelisted domain
and as the access token is in the url fragment after the # tag the server cant access it, only the client. and this is ensured to be yours erver as your domain must be whitelisted.
but it needs more client interaction...
well anyway. you can use the code i wrote for yuo and you can also do it in a popup. you just have to pass your variables to the form or whatever you are doing but this shouldnt be a problem. i did it this way because you said you needed it in php. a good javascript example can be found on the facebook connect page here: http://developers.facebook.com/docs/guides/web#login
any questions? comment!
ps: i put the # sign before the file_get_contents function because you might geht ssl errors, you should actually use curl for that and do error handling!
save this file as whatever.php to your server, check the 4 config variables in the top and hit it
<?
// your app id
$app_id = "123";
// your secret token
$mysecret = '***';
// your website correctly set up in facebook app config
$redirecturl = "http://www.yourdomain.com/whatever/thisfile.php?action=authorized";
// what you want to fetch
$scopes = array('email','user_interests'); // whatever you want
if($_GET['action'] == 'authorize') {
$url = "https://graph.facebook.com/oauth/authorize?client_id=".$app_id."&redirect_uri=";
$url .= urlencode($redirecturl);
$url .= "&display=popup";
$url .= "&scope=".implode(',',$scopes);
header("Location: $url");
exit();
} else if($_GET['action'] == 'authorized') {
$code = $_GET['code'];
$tokenurl = 'https://graph.facebook.com/oauth/access_token'.
'?client_id='.$app_id .
'&redirect_uri='.urlencode($redirecturl).
'&client_secret='.$mysecret.
'&code='.$code;
$token = #file_get_contents($tokenurl);
$token = preg_match('/access_token=(.*)&/',$token,$extracted_token);
$token = $extracted_token[1];
$dataurl = 'https://graph.facebook.com/me?access_token='.$token;
$result = #file_get_contents($dataurl);
$data = json_decode($result);
echo "<pre>".print_r($data,true)."</pre>";
} else {
?>click here to immport your data<?
}
?>

Categories