Skip to content

Commit

Permalink
Update standalone examples for new version
Browse files Browse the repository at this point in the history
  • Loading branch information
dapphp committed Dec 5, 2016
1 parent ec94a7e commit 8dd8257
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
14 changes: 5 additions & 9 deletions examples/display_value.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* Display Value Captcha Example
* 2012-04-18
*
* 2016-12-04
*
* This example shows how to use the "display_value" option in Securimage which
* allows the application to define the code that will be displayed on the
* captcha image.
Expand All @@ -19,7 +19,7 @@
ini_set('display_errors', 1);

// Defines Securimage class
require_once '../securimage.php';
require_once __DIR__ . '/../securimage.php';

// Create an array of options to give to Securimage
// This example sets the captcha text to the current time
Expand All @@ -34,12 +34,12 @@
// to the browser

$options = array('display_value' => date('h:i:s a'),
'captchaId' => sha1(uniqid($_SERVER['REMOTE_ADDR'] . $_SERVER['REMOTE_PORT'])),
'image_width' => 270,
'image_height' => 80,
'no_session' => true,
'no_exit' => true,
'use_database' => false,
'use_memcached' => false,
'send_headers' => false);

// construct new Securimage object with the given options
Expand All @@ -54,11 +54,7 @@

ob_start(); // start the output buffer
$img->show(); // output the image so it is captured by the buffer
$imgBinary = ob_get_contents(); // get contents of the buffer
ob_end_clean(); // turn off buffering and clear the buffer

header('Content-Type: image/png');
header('Content-Length: ' . strlen($imgBinary));

echo $imgBinary;

ob_end_flush(); // end output buffering and flush the image out
36 changes: 17 additions & 19 deletions examples/multiple_captchas_single_page.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php

/**
* Quick and dirty example of multiple captchas on one page using namespaces.
* Quick and dirty example of multiple captchas on one page.
*
* -- Synopsis --
* In order to add a captcha to multiple forms on a page, each image needs to
* have unique ID's for each <img> tag for the refresh button to work; they
* also need distinct names so the audio generation works properly.
*
* Most importantly, they must be assigned a namespace so the value of one
* image doesn't overwrite the other. With namespaces, the captcha values
* stored in the session or database are separated from eachother allowing
* multiple to exist on one page and be displayed simultaneously.
* Since version 4.0 - namespaces are no longer used or required. Each captcha
* is now identified by a unique ID which is automatically generated when using
* getCaptchaHtml().
*
*/

Expand All @@ -21,7 +20,7 @@
require_once __DIR__ . '/../securimage.php';
?>
<!doctype html>
<title>Captcha Namespaces - Multiple captchas on one page</title>
<title>Securimage :: Multiple captchas on one page</title>
<style>
body { text-align: center; padding: 150px; }
h3 { font-size: 20px; }
Expand All @@ -34,26 +33,26 @@
em.invalid { color: #f00; font-weight: bold; }
</style>

<h3>Multiple Captchas on one page using 'namespaces'</h3>

<article>
<h3>Multiple Captchas on one page using 'namespaces'</h3>

<fieldset>
<legend>"Contact Form" captcha...</legend>
<form method="POST" action="">
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<input type="hidden" name="action" value="contact_form">
<?php

$error_html1 = null;
$error_html1 = null; // no error

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'contact_form') {
// submitted "contact" form

// create new securimage object, indicating the namespace of the
// code we want to check based on what form was submitted.
$securimage = new Securimage(array('namespace' => 'contact'));
$securimage = new Securimage();

// validate user input
$valid = $securimage->check(@$_POST['captcha_code']);
$valid = $securimage->check(@$_POST['captcha_code'], $_POST['captcha_id']);

if ($valid) {
// code was correct, tell them so
Expand All @@ -66,10 +65,9 @@

// options controlling output of getCaptchaHtml()
$options1 = array(
'input_id' => 'contact_captcha', // ID of the text input field
'input_id' => 'contact_captcha', // ID of the text input field (must be unique on the page!)
'input_name' => 'captcha_code', // name of the captcha text field for POST
'image_id' => 'contact_captcha_img', // ID of the captcha image
'namespace' => 'contact', // namespace for storing and validating
'image_id' => 'contact_captcha_img', // ID of the captcha image (must be unique on the page!)
'error_html' => $error_html1, // error (or success) to display to the user above text input
);

Expand All @@ -86,7 +84,7 @@

<fieldset>
<legend>"Comments form" captcha...</legend>
<form method="POST" action="">
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<input type="hidden" name="action" value="comment">
<?php

Expand All @@ -95,8 +93,8 @@
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'comment') {
// submitted "contact" form

$securimage = new Securimage(array('namespace' => 'comments'));
$valid = $securimage->check($_POST['captcha_code']);
$securimage = new Securimage();
$valid = $securimage->check($_POST['captcha_code'], $_POST['captcha_id']);

if ($valid) {
$error_html2 = "<em class='valid'>Code entered correctly!</em><br>";
Expand All @@ -109,7 +107,6 @@
'input_id' => 'comments_captcha',
'input_name' => 'captcha_code',
'image_id' => 'comments_captcha_img',
'namespace' => 'comments',
'error_html' => $error_html2,
);

Expand All @@ -120,3 +117,4 @@
</form>
</fieldset>
</article>
</html>
23 changes: 13 additions & 10 deletions examples/test.mysql.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

require_once '../securimage.php';
require_once __DIR__ . '/../securimage.php';

$t0 = microtime(true);
$t0 = microtime(true);
$options = array(
'use_database' => true,
'database_driver' => Securimage::SI_DRIVER_MYSQL,
'database_host' => 'localhost',
'database_user' => 'securimage',
'database_pass' => 'password1234',
'database_name' => 'test',
'no_exit' => true,
);

$securimage = new Securimage(array('no_exit' => true));
$securimage->use_database = true;
$securimage->database_driver = Securimage::SI_DRIVER_MYSQL;
$securimage->database_host = 'localhost';
$securimage->database_user = 'securimage';
$securimage->database_pass = 'password1234';
$securimage->database_name = 'test';
$securimage = new Securimage($options);

$securimage->show();

$t1 = microtime(true);
$t1 = microtime(true);
22 changes: 13 additions & 9 deletions examples/test.pgsql.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

require_once '../securimage.php';

$securimage = new Securimage(array('no_exit' => true));
$securimage->use_database = true;
$securimage->database_driver = Securimage::SI_DRIVER_PGSQL;
$securimage->database_host = 'localhost';
$securimage->database_user = 'securimage';
$securimage->database_pass = 'password1234';
$securimage->database_name = 'test';
require_once __DIR__ . '/../securimage.php';

$options = array(
'use_database' => true,
'database_driver' => Securimage::SI_DRIVER_PGSQL,
'database_host' => 'localhost',
'database_user' => 'securimage',
'database_pass' => 'password1234',
'database_name' => 'test',
'no_exit' => true,
);

$securimage = new Securimage($options);

$securimage->show();
15 changes: 9 additions & 6 deletions examples/test.sqlite.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

require_once '../securimage.php';
require_once __DIR__ . '/../securimage.php';

$t0 = microtime(true);
$t0 = microtime(true);
$options = array(
'no_exit' => true,
'use_database' => true,
'database_driver' => Securimage::SI_DRIVER_SQLITE3,
);

$securimage = new Securimage(array('no_exit' => true));
$securimage->use_database = true;
$securimage->database_driver = Securimage::SI_DRIVER_SQLITE3;
$securimage = new Securimage($options);

$securimage->show();

$t1 = microtime(true);
$t1 = microtime(true);

0 comments on commit 8dd8257

Please sign in to comment.