0

I'm developing a site and I put the Facebook Connect using PHP SDK 3.

The problem is that the page where Facebook returns the results after the user authenticates and grants permission. For me after a user clicks "allow", it authenticates and then returns back to the same page (index) with a query string.

What I want is that if the user clicks "don't allow", he will be redirected to a certain page like login-fail.php, and if he grants permission he is redirected to login-success.php where his data will be processed and stored in the database.

Any ideas?

rgettman
  • 176,041
  • 30
  • 275
  • 357
med
  • 449
  • 2
  • 5
  • 16

2 Answers2

1

You cannot specify a redirect URL in case of error. The best you can do is to set a redirect URL that will be the same for success or error :

$args['redirect_uri'] = "http://www.yoursite.com/after-dialog.php"
$url = $facebook->getLoginUrl($args);

If the user clicks "Don't allow", he will be redirected to the page after-dialog.php with the following GET parameters :

error = access_denied
error_reason = user_denied
error_description = The+user+denied+your+request.

If you really want to redirect him according the success of his login, you can track it at the top of the after-dialog.php file by :

if (isset($_GET['error'])) {
  header('Location: http://www.yoursite.com/login-failed.php");
} else {
  header('Location: http://www.yoursite.com/login-success.php");
}

Hope that helps !


EDIT: As you pointed out (in the comments of this answer) the comments in the code of the SDK say :

redirect_uri: the url to go to after a successful login

But the reference of the API says :

If the user presses Don't Allow, your app is not authorized. The OAuth Dialog will redirect (via HTTP 302) the user's browser to the URL you passed in the redirect_uri parameter.

and :

If the user presses Allow, your app is authorized. The OAuth Dialog will redirect (via HTTP 302) the user's browser to the URL you passed in the redirect_uri parameter.

I also ran some tests and the user is always redirect to redirect_uri, even if he clicks "Don't allow". It must be a typo in the code comments.

Quentin
  • 2,529
  • 2
  • 26
  • 32
  • I found his on line 440 in base_facebook.php: * The parameters: * - redirect_uri: the url to go to after a successful login * - scope: comma separated list of requested extended perms I think it's possible to redirect: 'redirect_uri' => $currentUrl, // possibly overwritten – med May 28 '11 at 00:35
  • @med You are right ! Let me edit my answer end give you some code :) – Quentin May 28 '11 at 01:12
  • In fact, I have run some tests and you are redirect to the `redirect_uri` even if there is an error (the user clicks "Don't allow"). – Quentin May 28 '11 at 01:16
  • @med : See my edit in my answer. It looks like you still have to redirect manually the user... – Quentin May 28 '11 at 01:48
1

In my case I solved this problem with a checking of the parameter "error_reason", when an user click in Don't Allow, the url carry this parameter. You can see it in this page:

http://aplicacionesfacebookparadummies.blogspot.com/2011/09/obtener-datos-usuario-mediante-login.html

JGL
  • 11
  • 1