-
in php for user login from facebook
over 9 years ago
-
over 9 years ago
Priyanka, Facebook has its own SDK which allow user to login & signup via facebook
Refrence URL: https://github.com/facebook/facebook-php-sdk-v4
or you can use below code to integrate facebook login
- Paste JavaScript code in header or footer:
The below code is a JavaScript code; this function will call when user click on login fb button.
<script type="text/javascript"> window.fbAsyncInit = function() { FB.init( { appId: 'set_fb_app_Id', // App ID cookie: true, // enable cookies to allow the server to access the session xfbml: true, // parse XFBML oauth: true }); }; function loginViaFacebook() { FB.login(function(response) { if (response.authResponse) { // connected window.location.href = 'http://example.com/fbuserlogin'; // set your redirect url after login } else { // User cancelled login or did not fully authorize location.reload(true); } }, { scope: 'email,public_profile' //https://developers.facebook.com/docs/facebook-login/permissions/v2.2 } ); } (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); </script>
note: please replace _ with "_" while copying the above code.
- Use below html code to render facebook login button. < input type="button" value="Sign in with Facebook" onclick="javascript:loginViaFacebook();" /> < div id="fb-root">
- Please extract attached file & call it like this:- require_once('set_absolute_path/fb/src/facebook') then, use below PHP code to get user details after authentication from Facebook.
// Set facebook app credential $facebook = new Facebook(array( 'appId' => 'set_fb_app_Id', 'secret' => 'set_fb_secret' )); // Get User ID $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); // Get profile image $profile_image = $facebook->api('me/picture', array('redirect'=>false, 'height'=>'125', 'type'=>'normal', 'width'=>'125')); // Process your logic here } catch (FacebookApiException $e) { $user = null; } }
-
1 Answer(s)