Skip to content
Browse files

An example that proxies the CouchDB AuthSession cookie using setCooki…

…e() and getCookie().
  • Loading branch information...
1 parent 5991f72 commit 5850ac65ebf5ab94790361adf8fcca57cfcc76a5 @sbisbee committed Jul 17, 2011
Showing with 59 additions and 0 deletions.
  1. +4 −0 CHANGELOG
  2. +55 −0 examples/proxyCookie/index.php
View
4 CHANGELOG
@@ -9,6 +9,10 @@ New Features
* You can now set cookies that will be sent with every request using
setCookie(), and retrieve them with getCookie().
+ * An example of how to proxy the cookie based auth's AuthSession cookie from
+ your PHP layer to CouchDB, making it easier to have user accounts in your
+ application without rewriting user management yet again.
+
Version 0.5.0
-------------
View
55 examples/proxyCookie/index.php
@@ -0,0 +1,55 @@
+<?php
+require_once('../../src/Sag.php');
+
+session_start();
+
+try {
+ // We are going to get our page's content from Sag.
+ $sag = new Sag('sbisbee.com');
+ $sag->setDatabase('outlook');
+
+ if($_POST['login']) {
+ echo '<p>Using login()';
+
+ $_SESSION['AuthSession'] = $sag->login($_POST['username'], $_POST['password'], $sag::$AUTH_COOKIE);
+ }
+ else if($_SESSION['AuthSession']) {
+ echo '<p>Using setCookie()';
+
+ $sag->setCookie('AuthSession', $_SESSION['AuthSession']);
+ }
+
+ $result = $sag->get('/');
+}
+catch(Exception $e) {
+ $error = $e->getMessage();
+}
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
+ <title>proxyCookie example</title>
+ </head>
+ <body>
+ <?php
+ echo "<p>$error";
+
+ if($result) {
+ //Fancy display code.
+ var_dump($result);
+ }
+ else {
+ ?>
+ <form method="post" action="./index.php">
+ <input type="hidden" name="login" value="1"/>
+ <label for="username">Name</label> <input type="text" name="username"/><br/>
+ <label for="password">Password</label> <input type="password" name="password"/><br/>
+ <input type="submit" value="Login"/>
+ </form>
+ <?php
+ }
+ ?>
+ </body>
+</html>

0 comments on commit 5850ac6

Please sign in to comment.
Something went wrong with that request. Please try again.