Skip to content

Commit

Permalink
Adding a complete example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Aug 13, 2018
1 parent a96d8df commit 6d612ea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ You can also pass directly the wanted phrase to the builder:
$captcha = new CaptchaBuilder('hello');
```

Complete example
================

If you want to see an example you can have a look at he ``demo/form.php``, which uses ``demo/session.php`` to
render a captcha and check it after the submission

Symfony Bundle
================

Expand Down
29 changes: 29 additions & 0 deletions demo/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// We need the session to check the phrase after submitting
session_start();
?>

<html>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Checking that the posted phrase match the phrase stored in the session
if (isset($_SESSION['phrase']) && $_SESSION['phrase'] === $_POST['phrase']) {
echo "<h1>Captcha is valid !</h1>";
} else {
echo "<h1>Captcha is not valid!</h1>";
}
// The phrase can't be used twice
unset($_SESSION['phrase']);
}
?>
<form method="post">
Copy the CAPTCHA:
<?php
// See session.php, where the captcha is actually rendered and the session phrase
// is set accordingly to the image displayed
?>
<img src="session.php" />
<input type="text" name="phrase" />
<input type="submit" />
</form>
</html>
22 changes: 22 additions & 0 deletions demo/session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
// We need the session to store the correct phrase for later check
session_start();

// Including the autoload (you need to composer install in the main directory)
require_once __DIR__.'/../vendor/autoload.php';

use Gregwar\Captcha\CaptchaBuilder;

// Creating the captcha instance and setting the phrase in the session to store
// it for check when the form is submitted
$captcha = new CaptchaBuilder;
$_SESSION['phrase'] = $captcha->getPhrase();

// Setting the header to image jpeg because we here render an image
header('Content-Type: image/jpeg');

// Running the actual rendering of the captcha image
$captcha
->build()
->output()
;

0 comments on commit 6d612ea

Please sign in to comment.