Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

Zend Framework Google Login Example

Here is an example of how you can use Zend Framework to allow your users to log into your site using their Google Account access details.

<?php
 session_start();

 $email = isset($_REQUEST['email']) ? $_REQUEST['email'] : null;
 $password = isset($_REQUEST['password']) ? $_REQUEST['password'] : null;
 $captchaToken = isset($_REQUEST['captchatoken']) ? $_REQUEST['captchatoken'] : null;
 $captchaText = isset($_REQUEST['captchatext']) ? $_REQUEST['captchatext'] : null;

 if ($email && $password) {
  require_once 'Zend/Loader.php';
  Zend_Loader::registerAutoload();
  try {
   $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, 'xapi', null, 'Zend-ZendFramework', $captchaToken, $captchaText);
   $_SESSION['email'] = $email;
  } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
   $captchaToken = $e->getCaptchaToken();
   $captchaUrl = $e->getCaptchaUrl();
  } catch (Zend_Gdata_App_AuthException $e) {
   $error = $e->getMessage();
  }
 }

 if (isset($_SESSION['email'])) {
  header('Location: profile');
  exit;
 }
?>
<!DOCTYPE HTML>
<html lang="en">
 <head>
  <title>Zend Framework Google Login Example</title>
 </head>
 <body>
  <h1>Zend Framework Google Login Example</h1>
  <?php if (isset($error)): ?>
   <p><?php echo htmlspecialchars($error); ?></p>
  <?php endif; ?>
  <form method="post">
   <p><label for="email">Email:</label> <input id="email" name="email" type="text" value="<?php echo htmlspecialchars($email); ?>"></p>
   <p><label for="password">Password:</label> <input id="password" name="password" type="password"></p>
   <?php if (isset($captchaUrl)): ?>
    <div><input type="hidden" name="captchatoken" value="<?php echo htmlspecialchars($captchaToken); ?>"></div>
    <p><img src="<?php echo htmlspecialchars($captchaUrl); ?>" alt=""></p>
    <p><label for="captchatext">Text:</label> <input id="captchatext" name="captchatext" type="text"></p>
   <?php endif; ?>
   <p><button type="submit">Login</button></p>
  </form>
 </body>
</html>

I just don't want to have my password travel through somebody else's system. Surely there must be a better way? Can't we just do it like with Google App Engine?

3 Comments

Comment by OpenID calisza on Saturday, September 27, 2008 6:58:00 AM

Good example of what the Zend Framework can do. wd!

Comment by OpenID freebird on Saturday, September 27, 2008 9:13:00 AM

even i am looking for the same :-) not able to find anything till now.

Probably we can try modifying Oauth code available for cakephp

Comment by Blogger Charl van Niekerk on Saturday, September 27, 2008 8:26:00 PM

Thanks guys. Yeah the ZF GData PHP API libraries are even credited on Google's site.

Post a Comment

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.