Need to hide facebook login button on mobile devices using user agent check
Published: 2021-12-23 12:09:24

``<?php include_once("./header.php"); 

// Include the autoloader provided in the SDK
require_once './vendor/facebook/graph-sdk/src/Facebook
// Call Facebook API
$fb = new Facebook(array(
    'app_id' => FB_APP_ID,
    'app_secret' => FB_APP_SECRET,
    'default_graph_version' => 'v3.2',
));

// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();

// print_r($helper);

// Try to get access token
try {
    if(isset($_SESSION['facebook_access_token'])){
        $accessToken = $_SESSION['facebook_access_token'];
    }else{
          $accessToken = $helper->getAccessToken();
    }
} catch(FacebookResponseException $e) {
     echo 'Graph returned an error: ' . $e->getMessage();
      exit;
} catch(FacebookSDKException $e) {
    echo 'Facebook SDK returned an dcs ffdsfsdf: ' . $e->getMessage();
      exit;
}
       
       
if(isset($accessToken)){
    if(isset($_SESSION['facebook_access_token'])){
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }else{
        // Put short-lived access token in session
        $_SESSION['facebook_access_token'] = (string) $accessToken;
        
          // OAuth 2.0 client handler helps to manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();
        
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
        
        // Set default access token to be used in script
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }
    
    // Redirect the user back to the same page if url has "code" parameter in query string
    if(isset($_GET['code'])){
        header('Location: ./');
    }
    
    // Getting user's profile info from Facebook
    try {
        
        $profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
        // $requestPicture = $fb->get('/me/picture?redirect=false&height=200'); //getting user picture
        // $picture = $requestPicture->getGraphUser();
        $profile = $profile_request->getGraphUser();
        $fbid = $profile->getProperty('id');           // To Get Facebook ID
        $fbfullname = $profile->getProperty('name');   // To Get Facebook full name
        $fbemail = $profile->getProperty('email');    //  To Get Facebook email
        $fbpic = $picture['url'];
        // $fbpic1 = "<img src='".$picture['url']."' style='border-radius:50%;height:30px;width:30px;'/>";
        $gender='';
        $_SESSION['fb_id'] = $fbid.'</br>';
        $_SESSION['fb_name'] = $fbfullname.'</br>';
        $_SESSION['fb_email'] = $fbemail.'</br>';
        // $_SESSION['fb_pic'] = $fbpic1.'</br>';
        
        print_r($profile);
        
        $oauth_provider='facebook';
        $qry1 = "SELECT * FROM users WHERE oauth_id = '".$fbid."' AND user_status = 1";

	$arr_user1 = mysql_query($qry1);

	$count1 = mysql_num_rows($arr_user1) ;
	$user_det = mysql_fetch_array($arr_user1) ;
	if($count1 == 1)
	{
	$_SESSION['user']['log']['id']=	$_SESSION['log']['user_id'] = $_SESSION["user_id"] = $user_det["user_id"];
		$_SESSION["user_name"] = $user_det["user_firstname"];
		$_SESSION["user_lastname"] = $user_det["user_lastname"];
		$_SESSION["user_email"] = $user_det["user_email"];
		$_SESSION["user_mobile"] = $_SESSION["usr"]["mobile"] = $user_det["user_mobileno"]; 
		$_SESSION["accessing_LogID"] = $_SESSION["user_id"];
		$_SESSION['accessing_type'] = $user_det['user_typeID'];	
		header("location:index");
		exit();
	}
	else 
	{
	    $qry = "insert into users(user_email,user_firstname,oauth_id,image,user_typeID,user_status) value('".mysql_real_escape_string($fbemail)."','".mysql_real_escape_string($fbfullname)."','".mysql_real_escape_string($fbid)."', '".mysql_real_escape_string($fbpic)."',3,1)" ;
	    $sql = mysql_query($qry);
		
	$uid = mysql_insert_id() ; 
		 $qry_log = "SELECT * FROM users WHERE user_id = '".$uid."' AND user_status = 1";

	$arr_users1 = mysql_query($qry_log);

//	$count1 = mysql_num_rows($arr_user1) ;
	$user_dets = mysql_fetch_array($arr_users1) ;
	$_SESSION['user']['log']['id']=	$_SESSION['log']['user_id'] = $_SESSION["user_id"] = $user_dets["user_id"];
		$_SESSION["user_name"] = $user_dets["user_firstname"];
		$_SESSION["user_lastname"] = $user_dets["user_lastname"];
		$_SESSION["user_email"] = $user_dets["user_email"];
		$_SESSION["user_mobile"] = $_SESSION["usr"]["mobile"] = $user_dets["user_mobileno"]; 
		$_SESSION["accessing_LogID"] = $_SESSION["user_id"];
		$_SESSION['accessing_type'] = $user_dets['user_typeID'];	
		header("location:index");
		exit();
	}

        // echo($_SESSION['fb_pic']);
        // exit;
        
    } catch(FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        session_destroy();
        // Redirect user back to app login page
        header("Location: ./");
        exit;
    } catch(FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    
    // $_SESSION['userData'] = $fbUser;
    
    
    // print_r($fbUser['id']);
    
     // Get logout url
    $logoutURL = $helper->getLogoutUrl($accessToken, FB_REDIRECT_URL.'logout.php');
       
        //   print_r("if condition");

}else{
    
    // print_r("else condition");
    // Get login url
    $permissions = ['email']; // Optional permissions
    $loginURL = $helper->getLoginUrl(FB_REDIRECT_URL, $permissions);
    //  print_r($loginURL);
       
 $output = '<a href="'.htmlspecialchars($loginURL).'" target="_blank"><img class="mark-lazy" src="images/facebook-sign-in-button.png" alt="fb"></a>';
 
}

?>
  <?php echo $output; ?>
 
    <a href="<?php echo $loginbutton; ?>" target="_blank"><img class="mark-lazy" src="images/google_images.png" alt="gg+"></a>
	</div>
</div>

</div>
</div>

<?php include_once("includes/footer.php"); ?>