Jumat, 02 Oktober 2015

Create A Registration Form By Google With PHP Script

Create A Registration Form By Google With PHP Script - Creating the registration form using the Google service will provide convenience for users of your website. Only with a single click a user of the website is able to register using their Google email which must be equipped with photo ID that complies with their Google account.
Create A Registration Form
The following services are safe service created by Google using OAuth2 protocol. It will process the direct exchange of data between the server that you have with Google. but we will leave all this to Google’s PHP Library, whcih will handle nearly everything on the server side.
How to login / register to use the Google service has many advantages, including:
  • each new registrant does not need validation troublesome.
  • there should be no forgetting "password".
  • You will get a complete data registries in the form of emails and photos with just one click.
  • The email you get already get validation from Google, so it does not need to be revalidated.
  • Great security on Google's part.
The first step is to create a new application through Google’s API Console. Follow these instructions for more information. After you complete the process, place the obtained keys in setup.php.
First let's make a php script that we can name it index.php.
require 'setup.php';

// Create a new Google API client
$client = new apiClient();
//$client->setApplicationName("Tutorialzine");

// Configure it
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setDeveloperKey($api_key);
$client->setRedirectUri($redirect_url);
$client->setApprovalPrompt(false);
$oauth2 = new apiOauth2Service($client);

// The code parameter signifies that this is
// a redirect from google, bearing a temporary code
if (isset($_GET['code'])) {

 // This method will obtain the actuall access token from Google,
 // so we can request user info
 $client->authenticate();

 // Get the user data
 $info = $oauth2->userinfo->get();

 // Find this person in the database
 $person = ORM::for_table('glogin_users')->where('email', $info['email'])->find_one();

 if(!$person){
  // No such person was found. Register!

  $person = ORM::for_table('glogin_users')->create();

  // Set the properties that are to be inserted in the db
  $person->email = $info['email'];
  $person->name = $info['name'];

  if(isset($info['picture'])){
   // If the user has set a public google account photo
   $person->photo = $info['picture'];
  }
  else{
   // otherwise use the default
   $person->photo = 'assets/img/default_avatar.jpg';
  }

  // insert the record to the database
  $person->save();
 }

 // Save the user id to the session
 $_SESSION['user_id'] = $person->id();

 // Redirect to the base demo URL
 header("Location: $redirect_url");
 exit;
}

// Handle logout
if (isset($_GET['logout'])) {
 unset($_SESSION['user_id']);
}

$person = null;
if(isset($_SESSION['user_id'])){
 // Fetch the person from the database
 $person = ORM::for_table('glogin_users')->find_one($_SESSION['user_id']);
}
The HTML5
The following script will be useful to display the form on your website typing someone would login or register.
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Google Powered Login Form | Tutorialzine Demo</title>

        <!-- The stylesheets -->
        <link rel="stylesheet" href="assets/css/styles.css" />
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700" />

        <!--[if lt IE 9]>
          <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    </head>

    <body>

  <h1>Login Form</h1>
        <div id="main">

   <?php if($person):?>
    <div id="avatar" style="background-image:url(<?php echo $person->photo?>?sz=58)"></div>
    <p class="greeting">Welcome, <b><?php echo htmlspecialchars($person->name)?></b></p>
             <p class="register_info">You registered <b><?php echo new RelativeTime($person->registered)?></b></p>
             <a href="?logout" class="logoutButton">Logout</a>
   <?php else:?>
             <a href="<?php echo $client->createAuthUrl()?>" class="googleLoginButton">Sign in with Google</a>
            <?php endif;?>

        </div>

    </body>
</html>
Hopefully this short article can be taken advantage and for more information you can download it directly.
Keyword SearchCreate A Registration Form By Google With PHP Scripthow to make registration form in phphow to make registration form in php and mysql, Create A Registration Form.

Create A Registration Form By Google With PHP Script Rating: 4.5 Diposkan Oleh: Kang Icung

0 komentar:

Posting Komentar