Login with Facebook and Google accounts in PHP

    Rating: ★★★★☆
    View: 288
    Download: 182
    Từ khóa:

    For this module,We need the Client ID,Client Secret and Registered Redirect Uri for Google and App Id and App Secret for Facebook .For Google one Please refer this article Create a Google OAuth2.0 Client ID. And  for the Facebook app creation please refer this article  Creation of the Facebook App

    After getting the Google OAuth2.0 app details and facebook app details,define these app details in config.php file.

    In the config.php file,we defined the Access Type is offline.Basically Google OAuth2.0 provides two types of access which are offline and online. And also here we defineApproval Prompt as ‘auto‘. In this script, the default value for the Approval Prompt is ‘force‘ and for the Access Type is ‘offline‘.

    Here below shows the index.php file code.

    
    
    
    
        Signin with Social Accounts-Idiot Minds
    
    
    
     
    
    
     
     
    

    In this index.php file, we used the one oauthpopup jQuery plugin for Popup and Google logout. For logout,when click on this “Logout” link it will logout from the Google account after that it will redirect to specified url which is defined in index.php file like shown in below.

    $(document).ready(function(){
    //For Gmail
        $('a.login').oauthpopup({
            path: 'login.php?google',
            width:650,
            height:350,
        });
        $('a.google_logout').googlelogout({
            redirect_url:'logout.php?google'
        });
    //For Facebook
      $('#facebook').oauthpopup({
            path: 'login.php?facebook',
            width:600,
            height:300,
       });
     
    });

    Here below one is the login.php file 

    require 'Social.php';
        $Social_obj= new Social();
    if(isset($_GET['facebook'])){
        $Social_obj->facebook();
    }
    if(isset($_GET['google'])){
        $Social_obj->google();
    }
     
    ?>
    
    

    In login.php file, we are using the Social.php class for authentication.In this file we pass the all the credentials for authentication which are defined in the Config.php file.

      APP_ID,
    		'secret'	=> APP_SECRET,
    		));
    			//get the user facebook id		
    			$user = $facebook->getUser();
    			//echo $user;exit;
    			if($user){
    				try{
    					//get the facebook user profile data
    					$user_profile = $facebook->api('/me');
    					$params = array('next' => BASE_URL.'logout.php');
    					//logout url
    					$logout =$facebook->getLogoutUrl($params);
    					$_SESSION['User']=$user_profile;
    					$_SESSION['facebook_logout']=$logout;
    				}catch(FacebookApiException $e){
    					error_log($e);
    					$user = NULL;
    				}		
    			}
    			if(empty($user)){
    			  //login url	
    			  $loginurl = $facebook->getLoginUrl(array(
    							'scope'	=> 'email,read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
    							'redirect_uri'	=> BASE_URL.'login.php?facebook',
    							'display'=>'popup'
    							));
    			  header('Location: '.$loginurl);
    			}
     
      }
        function google(){
     
    			$client = new Google_Client();
    			$client->setApplicationName("Idiot Minds Google Login Functionallity");
    			$client->setClientId(CLIENT_ID);
    			$client->setClientSecret(CLIENT_SECRET);
    			$client->setRedirectUri(REDIRECT_URI);
    			$client->setApprovalPrompt(APPROVAL_PROMPT);
    			$client->setAccessType(ACCESS_TYPE);
    			$oauth2 = new Google_Oauth2Service($client);
    			if (isset($_GET['code'])) {
    			  $client->authenticate($_GET['code']);
    			  $_SESSION['token'] = $client->getAccessToken();
    			}
    			if (isset($_SESSION['token'])) {
    			 $client->setAccessToken($_SESSION['token']);
    			}
    			if (isset($_REQUEST['error'])) {
    			 echo ''; exit;
    			}
    			if ($client->getAccessToken()) {
    			  $user = $oauth2->userinfo->get();
    			  $_SESSION['User']=$user;
    			  $_SESSION['token'] = $client->getAccessToken();
     
    			} else {
    			  $authUrl = $client->createAuthUrl();
    			  header('Location: '.$authUrl);
     
    			}
          }
     
    }
     
    ?>

    In the logout.php file,we unset the OAuth2 Access token and Revoke an OAuth2 access token and also for facebook we destroy the entire session .

    revokeToken();    
    }
     
    if(isset($_SESSION['User']) && !empty($_SESSION['User'])){
    session_destroy();
    }
    header('Location: '.BASE_URL);
    ?>

     

    loading Đang tải...

    template được ưa chuộng