Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
static_captcha.php
1 <?php
2 
3 /**
4  * Static Captcha Example Script
5  * 2012-04-18
6  * @version 3.2RC2 (April 2012)
7  *
8  * The static captcha exposes an easy to use interface that applications can
9  * use to generate captcha challenges and validate them by a unique ID. A
10  * captcha image can be associated with an ID and no PHP sessions are required.
11  * The captcha ID can be stored in a SQLite database by Securimage.
12  *
13  * Tip: To give the user a refresh captcha button, use Ajax to request a new ID,
14  * update the hidden form input with the new captcha ID, and update the image source
15  * to securimage_show.php providing the captcha ID.
16  */
17 
18 // set debugging
19 error_reporting(E_ALL);
20 ini_set('display_errors', 1);
21 
22 // defines Securimage class
23 require_once '../securimage.php';
24 
25 // get the captcha ID from the url (if supplied)
26 $captchaId = (isset($_GET['id'])) ? $_GET['id'] : '';
27 
28 // if the validate option is set
29 if (isset($_GET['validate'])) {
30  // get the user input of the captcha code
31  $input = (isset($_GET['input'])) ? $_GET['input'] : '';
32 
33  // call Securimage::checkCaptchaId to validate input
34  // returns true if the code and id are a valid pair, false if not
35  if (Securimage::checkByCaptchaId($captchaId, $input) == true) {
36  echo "<h2>Success</h2>"
37  ."<span style='color: #33cc00'>The captcha code entered was correct!</span>"
38  ."<br /><br />";
39  } else {
40  echo "<h2>Incorrect Code</h2>"
41  ."<span style='color: #f00'>Incorrect captcha code, try again.</span>"
42  ."<br /><br />";
43  }
44 
45 } else if (isset($_GET['display'])) {
46  // display the captcha with the supplied ID from the URL
47 
48  // construct options specifying the existing captcha ID
49  // also tell securimage not to start a session
50  $options = array('captchaId' => $captchaId,
51  'no_session' => true);
52  $captcha = new Securimage($options);
53 
54  // show the image, this sends proper HTTP headers
55  $captcha->show();
56  exit;
57 }
58 
59 // generate a new captcha ID and challenge
60 $captchaId = Securimage::getCaptchaId();
61 
62 // output the captcha ID, and a form to validate it
63 // the form submits to itself and is validated above
64 echo <<<EOD
65  <!DOCTYPE html>
66  <html>
67  <head>
68  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
69  <title>Static Captcha Example</title>
70  </head>
71  <body>
72  <h2>Static Captcha Example</h2>
73 
74  <div>
75  Synopsis:
76  <ul>
77  <li>Request new captchaId using <em>Securimage::getCaptchaId()</em></li>
78  <li>Display form with hidden field containing captchaId</li>
79  <li>Display captcha image passing the captchaId to the image</li>
80  <li>Validate captcha input against captchaId using <em>Securimage::checkByCaptchaId()</em></li>
81  </ul>
82  </div>
83  <p>&nbsp;</p>
84  <div>
85  Captcha ID: $captchaId<br /><br />
86  <img src="{$_SERVER['PHP_SELF']}?display&amp;id=$captchaId" alt="Captcha Image" /><br />
87 
88  <form method="get" action="{$_SERVER['PHP_SELF']}">
89  <input type="hidden" name="validate" value="1" />
90  <input type="hidden" name="id" value="$captchaId" />
91  Enter Code:
92  <input type="text" name="input" value="" />
93  <input type="submit" name="submit" value="Check Captcha" />
94  </form>
95  </div>
96  </body>
97  </html>
98 EOD;