Search code examples
facebookfacebook-php-sdk

Facebook Connect API callback URL


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?


Solution

  • 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.