问题
I'm develpping 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?
回答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.
回答2:
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
来源:https://stackoverflow.com/questions/6158008/facebook-connect-api-callback-url