Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created February 28, 2012 10:30
Show Gist options
  • Save k-holy/1931780 to your computer and use it in GitHub Desktop.
Save k-holy/1931780 to your computer and use it in GitHub Desktop.
Volcanus\TokenProcessor
<?php
namespace Acme;
use Volcanus\TokenProcessor;
class U
{
public static function H($data, $filter = null) {
$var = (isset($data))
? htmlspecialchars($data, ENT_QUOTES, 'UTF-8')
: null;
return (is_callable($filter))
? $filter($var)
: $var;
}
}
require_once realpath(__DIR__ . '/../vendor/Volcanus_Utils/src/Volcanus/TokenProcessor.php');
ini_set('url_rewriter.tags', 'form=');
$tokens = array();
session_start();
if (isset($_SESSION['TOKENS'])) {
$tokens = &$_SESSION['TOKENS'];
} else {
$_SESSION['TOKENS'] = &$tokens;
}
$name = '__TRANSACTION_TOKEN';
$p = new TokenProcessor(array(
'name' => $name,
'lifetime' => 1800,
'capacity' => 5,
));
$p->setTokens($tokens);
$token_value = (isset($_POST['submit']) && isset($_POST[$name]))
? $_POST[$name] : null;
$messages = array();
if (isset($token_value)) {
if (!$p->check($token_value)) {
$messages[] = 'INVALID TOKEN...';
} else {
$messages[] = 'VALID TOKEN!!';
}
}
$token = $p->generate();
output_add_rewrite_var($token['name'], $token['value']);
$title = 'TokenProcessor & output_add_rewrite_var()';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><?=U::H($title)?></title>
</head>
<body>
<h1><?=U::H($title)?></h1>
<?php if (count($messages) >= 1) : ?>
<?php foreach ($messages as $message) : ?>
<pre><?=U::H($message)?></pre>
<?php endforeach ?>
<?php endif ?>
<form method="post">
<input type="submit" name="submit" value="送信" />
</form>
<pre>
<?=U::H(print_r($tokens, true))?>
</pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment