PHP tutorials,a way to getting some thing new in web

Followers

Powered by Blogger.

Monday 4 August 2014

Login with Google using PHP

No comments :
You can create your website with google login.You can now socialize your website on google. here is code for it:

Please get google api client from google site

Please make sure you create application for google application on google developers.


Here is code:

<?php
@session_start();
require_once 'src/apiClient.php';
require_once 'src/contrib/apiOauth2Service.php';

$client = new apiClient();
$client->setApplicationName("Google Account Login");
$oauth2 = new apiOauth2Service($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
  unset($_SESSION['google_data']); //Google session data unset
  $client->revokeToken();
}

if ($client->getAccessToken()) {
  $user = $oauth2->userinfo->get();
   if (!empty($user ))
  {
      echo "<strong>Your Google details follows.</strong><br/>";
        echo "Name : ".$user['name']."<br/>";
        echo "Google ID : ".$user['id']."<br/>";
        echo "Google Email : ".$user['email']."<br/><br/><br/><br/>";
  }
 


  $_SESSION['google_data']=$user;
   

  $_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>

<?php if(isset($personMarkup)): ?>
<?php print $personMarkup ?>
<?php endif ?>
<?php
  if(isset($authUrl)) {
    print "<a class='login' href='$authUrl'>Google Account Login</a>";
  } else {
   print "<a class='logout' href='?logout'>Logout</a>";
  }
?>
<a href="http://friendstaxi.com/" style="font-size:24px; color:#C00; font-family:Arial, Helvetica, sans-serif;  margin:100px;float:left">Goto HOME</a>


No comments :

Post a Comment