-
Notifications
You must be signed in to change notification settings - Fork 1
/
phtml-easy-form.php
65 lines (60 loc) · 1.7 KB
/
phtml-easy-form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
require __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Cache\Simple\FilesystemCache;
use \pedroac\nonce\NoncesManager;
use \pedroac\nonce\Form\HtmlNonceField;
use \pedroac\nonce\Form\NonceForm;
const STATE_NOT_SUBMITTED = 1;
const STATE_SUCCESS = 2;
const STATE_INVALID_INPUT = 3;
const STATE_INVALID_TOKEN = 4;
$isValidForm = false;
$state = STATE_NOT_SUBMITTED;
$inputNumber = filter_input(INPUT_POST, 'number');
/**
* Create the nonce form manager.
*/
$form = new NonceForm(
'token',
new NoncesManager(new FilesystemCache)
);
/**
* Validate form input.
*/
if ($form->isSubmittedInvalid()) {
$state = STATE_INVALID_TOKEN;
} else if ($form->isSubmittedValid()) {
$isValidForm = is_numeric($inputNumber);
$state = $isValidForm ? STATE_SUCCESS : STATE_INVALID_INPUT;
}
/**
* Create an HTML nonce field generator.
*/
$htmlField = new HtmlNonceField($form);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
</head>
<body>
<?php if ($state == STATE_INVALID_TOKEN) : ?>
<p>Invalid token!</p>
<?php elseif ($state == STATE_SUCCESS) : ?>
<p>Success! Resending the form will throw an error.</p>
<?php else : ?>
<?php if ($state == STATE_INVALID_INPUT) : ?>
<p>Invalid input</p>
<?php endif; ?>
<form method="POST">
Number:
<input type="text"
name="number"
value="<?= $inputNumber ?>" />
<?= $htmlField ?>
<input type="submit" name="myform" value="Submit" />
</form>
<?php endif; ?>
</body>
</html>