PHP SDK + Code Igniter - php

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.

Related

PHP - Go back to previous URL after login with Facebook

I have created a facebook login using the tutorial from http://www.9lessons.info/2011/02/login-with-facebook-and-twitter.html. But the problem is after logging in the user will be redirected to the index.php page. How can i redirect him to the page from which he has clicked the login.
I tried saving the previous url to session. But i don't know why, its not working. It worked for normal login, but not for facebook. I also tried saving the previous url to cookie. It also didn't work. Url is saving, But after logging in session url/cookie url is lost. Can someone pls tell me an alternative method???
Example to store the current page
Do on all but your login page before redirect:
session_start();
$_SESSION['lastpage'] = $_SERVER['HTTP_REFERER'];
On all other pages :
session_start();
if(isset($_SESSION['lastpage'])) {
$lastpage = $_SESSION['lastpage'];
$_SESSION['lastpage'] = false;
unset($_SESSION['lastpage']);
header("location: " . $lastpage);
}
Something like that should work. Make sure to santize the $_SESSION variable, and also validate the last URL was from your site (or supported site) before setting the session var.

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.

How to clear the data of Facebook App object

I have visited this link but cant get any help
$facebook->getUser() ALWAYS Returning ID - Can't Logout
the problem is when $facebook->getLogoutUrl is executed it just logout the user from facebook means destroy the facebook session but data return by the app is still there on the page. how to clear that data.
what i want is when logout link is clicked than the data on my app page also clear and login with facebook link appears.
here is the link to my example page
http://www.webdesigncc.info/demoz/myfacebook/examples/example.php
This may solve your problem
function facebook_logout()
{
$logout = $facebook->getLogoutUrl(array('next'=>'your url to be redirected after logout'));
$facebook->destroySession(); /*To destroy facebook sessions.
Available in facebook php sdk*/
$session_destroy(); //Optional if you want to destroy your site's sessions
header("Location:$logout");
}
Using the following way you can clear all datas.
public function destroySession() {
$this->setAccessToken(null);
$this->user = 0;
$this->clearAllPersistentData();
}

CodeIgniter - Is my custom session data being stripped by Facebook?

I'm wondering if there's a way to dump all of the values of
$this->session->userdata()
so I can troubleshoot?
I'm working within Facebook, and have a login page, and once that's successful I want to pass around the UID of the current user, and I thought this would work well.
I currently have the uid set as follows:
require_once 'facebook.php';
$appapikey = 'XXXX';
$appsecret = 'XXXX';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();
$this->db->like('uid', $user_id);
$this->db->from('users');
$has_signed_up = $this->db->count_all_results();
if ($has_signed_up == 0) {
redirect('/setup/signup/', 'location');
}
else {
$this->session->set_userdata('uid', $user_id);
redirect('/preferences/index/', 'location');
}
So the redirection occurs, and I have a very simple setup over at preferences/index:
echo "this is the preferences form <br />";
echo $this->session->userdata('uid');
echo $this->session->userdata('session_id');
And the result is an inscrutable:
this is the preferences form
858f500e167e359edc1942a96f3bac35
So it totally skips over the middle echo containing the uid. Am I not setting this correctly? Is there a way to dump all values of the session array to see what's getting through? Any help would be just great.
UPDATE
I have run var_dump($this->session->userdata) on each the raw website and through Facebook.
On the website it exposes all set values in an array containing 5 values (session_id, IP, User_agent, last_activity, and uid).
Within the Facebook chrome however, it only shows the 4 values set by CodeIgniter. I've heard cookies can only be 4k and that encryption could be a problem. Could FB be filling up cookies with its own encrypted (read:larger) data?
UPDATE 2
When I comment out the redirect, and just have:
else {
$this->session->set_userdata('uid', $user_id);
echo ':test_'.$this->session->userdata('uid').'_test:';
//redirect('/preferences/index/', 'location');
}
It dutifully returns :test_1234_test: within Facebook. So somewhere during the redirect it's losing this part of the array (but not the whole array).
Is it possibly just creating a new session on the redirect page? So that's why it only has the four "stock" variables? If this is the case, I'll need to research how it creates the sessions, and if Facebook clears cookies I suppose.
UPDATE 3
So I've turned to using a DB to store session information instead of cookies, thinking FB was either stripping them or colliding with them. I currently have the app set up to
Set $user_id = 1234
$this->session->set_userdata('uid', $user_id)
Redirect to the new page
Var_dump all possible information
What occurs in the DB is this:
DB records http://nikolausjj.facebook.joyent.us/Picture2.png
So it creates one good record, with the user data, then immediately upon the redirect creates a new session without recognizing the prior one. Can someone explain where the CI framework checks to see if it has a prior session existing? The user manual explains it as "magic" basically.
You can use var_dump() to output the session. Something like this
var_dump($this->session);
The set_userdata call looks ok. Are you sure $user_id is set. Because the echo is surley executed but uid isn't set or set to empty string.
Try replacing the echo with
echo ':test_'.$this->session->userdata('uid').'_test:';
Other information helpful for answering
What browser are you using?
Do you have an underscore _ in your domain name?
Are you using CI sessions or some wrapper for native PHPsessions
Is the value for uid also lost/not set when you comment out the redirect?
Other suggestions:
try redirect('/preferences/index/', 'refresh'); instead of location
I'm not familiar with facebook development but is /preferences/index under your control? If yes try removing (if present) $this->load->library(‘session’) and instead load it in autoload.php.
try changing $config[‘sess_match_ip’] to `FALSE
try setting $config[‘sess_encrypt_cookie’] to FALSE
try replacing the use of CI-Session with CI Native session
Is UID sensible information if not store it in a cookie. If it matters if it can be spoofed don't.
I didn't solve how to pass session variables from one page to another via Facebook. My solution was simply to call the Facebook API for the user's UID again on each page. Not great programming, but it works alright for me. :-/

problem with facebook api - Keeps redirecting

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();

Categories