简体   繁体   中英

how to redirect the user to the previous link after login using facebook

I'm using facebook login in my website where the user can login from different pages everything works fine just one thing how to redirect the user to the previous url after login which will be different depond on where the user click login

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;
    use Facebook\FacebookHttpable;
    use Facebook\FacebookCurlHttpClient;
    use Facebook\FacebookCurl;
        $app_id='XXXXXXXXX';
        $app_sercret='XXXXXXXXXXXX';
         $redirect_url='http://example.com';

        //Intialize
        FacebookSession::setDefaultApplication($app_id,$app_sercret);
        $helper= new FacebookRedirectLoginHelper($redirect_url);
        $sess=$helper->getSessionFromRedirect();


        if(isset($_SESSION['fb_token'])){
                $sess = new FacebookSession($_SESSION['fb_token']);
            }
        //if exisit
           if(isset($sess)){
             $_SESSION['fb_token']= $sess->getToken();
             $request = new FacebookRequest($sess,'GET','/me');
             $response =$request->execute();
             $graph    = $response->getGraphObject(GraphUser::classname());
             $name     = $graph->getName();
             $id        = $graph->getId();
             $pimage     = 'http://graph.facebook.com/'.$id.'/picture?width=170';
             $timage     = 'http://graph.facebook.com/'.$id.'/picture?width=30';
             $email     = $graph->getproperty('email');
             $attributes=array(
            "user_fname" => $name,
            "user_email" => $email,
            "user_image" => $pimage,
            "user_thumb" => $timage,
        );
           $newuser   =Details::create($attributes);

When you are on your PHP page where you redirect users to your Facebook login page, save your user's last loaded page in session by using $_SERVER['HTTP_REFERER'] ( source ).

Once user logs in and Facebook redirects to your callback URL, fetch that URL from your session and redirect user to that page.

For the facebook login you need to create an app in facebook developers page and then need to give a valid redirect link in that app. The below code,

$helper->getSessionFromRedirect();

checks from your code redirect link provided in constructor FacebookRedirectLoginHelper() is present in the app or not.

So basically add this link in the variable $redirect_url that you provided in code to your facebook app.

You do not need to worry about redirect after login is done successfully on facebook, it is the job of facebook to redirect you back to the valid url that you provide.

If you think to redirect user to another page after he comes back from facebook, then create config variables to store different redirect url and use header() function to redirect them depending upon your conditions.

Use this code...

$return_url = base64_decode($_GET["return_url"]); //return url
header('Location:'.$return_url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM