finLprtoTyp
finLprtoTyp

Reputation: 191

FB PHP SDK and mod_rewrite: getUser returns 0

I am using the Facebook PHP SDK in order to allow users to log in to my site using Facebook.
In my test below (assume the URL is http://mysite.com/test/fbtest.php),

<?php
    require_once("facebook.php");
    $fb = new Facebook(array('appId' => 'APP_ID', 'secret' => 'APP_SECRET'));
    $fbuser = false;
    $fbuserid = $fb->getUser();
    $fblogin = $fb->getLoginUrl(array(
        'redirect_uri' => "http://{$_SERVER['HTTP_HOST']}/test/fbtest.php"));

if($fbuserid)
{
    try
    {
        $fbuser = $fb->api("/me");
        print_r($fbuser);
        echo "<br/>\n";
    }
    catch (FacebookApiException $e)
    {
        $fbuser = false;
        $fbuserid = 0;
    }
}
if(!$fbuser)
    echo "<a href=\"$fblogin\">FB Login</a>\n";
?>

This seems to work as expected. However, when I add the following rewrite rule,

RewriteRule ^/FBtest/(.*)$ http://%{HTTP_HOST}/test/fbtest.php$1

Then change my login redirect to the following,

$fblogin = $fb->getLoginUrl(array(
        'redirect_uri' => "http://{$_SERVER['HTTP_HOST']}/FBtest/"));

Then $fb->getUser() always returns 0. I feel that I am missing something important.

Upvotes: 1

Views: 663

Answers (3)

finLprtoTyp
finLprtoTyp

Reputation: 191

Finally figured this problem out. Here is the RewriteRule I need:

RewriteRule ^/FBtest/$ http://%{HTTP_HOST}/test/fbtest.php [QSA,NE]

It turns out I needed to add both the QSA and NE flags to this rule.

Much like in CBroe's answer, the QSA flag was needed for the state/code parameters added to redirect_uri (using (.*) within the RewriteRule instead doesn't catch these additional parameters).

I also needed to add the NE flag because the state/code that the Facebook authentication was adding to the redirect_uri was being escaped.

Upvotes: 1

C3roe
C3roe

Reputation: 96383

In the server side flow, your redirect_uri get’s called with the necessary values as GET parameters in the query string.

'redirect_uri' => "http://{$_SERVER['HTTP_HOST']}/FBtest/"

So with this redirect_uri, something like

http://example.com/FBtest/?state=foo&code=bar

will be called on your server.

RewriteRule ^/FBtest/(.*)$ http://%{HTTP_HOST}/test/fbtest.php$1

RewriteRules don’t examine the query string, they only look at the path component of the URL. In your case, that’s /FBtest/, nothing behind it – so the internal redirect goes to /test/fbtest.php, and the query string parameters get lost, because you didn’t say you wanted to pass them on.

Add the flag [QSA] – for “query string append” – to your RewriteRule (and remove the unnecessary (.*)) – then things should work as expected, because your fbtest.php will get the query string parameters needed for the auth process.

Upvotes: 1

ShawnDaGeek
ShawnDaGeek

Reputation: 4150

You will not be able to use any other domain other than the domain you set in your applications settings.

"be sure you are using the root domain, and not sample.com/test/ as your url settings."

Upvotes: 0

Related Questions