Csrf
in package
CSRF protection class.
Tags
Table of Contents
Properties
- $CsrfClass : SlimGuard
- $options : array<string|int, mixed>
Methods
- __construct() : mixed
- CSRF protection.
- __get() : mixed
- Magic get.
- createToken() : array<string|int, mixed>
- Create token.
- getInstance() : SlimGuard
- Get CSRF class instance.
- getTokenNameValueKey() : array<string|int, mixed>
- Get token name key and value key.
- validateToken() : bool
- Validate CSRF token.
Properties
$CsrfClass
protected
SlimGuard
$CsrfClass
$options
protected
array<string|int, mixed>
$options
= []
The associative array options. See options
parameter in class constructor.
Methods
__construct()
CSRF protection.
public
__construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
-
The associative array with keys:
'prefix' (string) For allow create and validate token with difference form actions.
'failureCallable' (callable) callable to be executed if the CSRF validation fails.
'storage' (null|array|ArrayAccess) Should be either an array or an object. If an object is used, then it must implement ArrayAccess and should implement Countable and Iterator if storage limit enforcement is required. 'storageLimit' (int) For limit number of token (re)generate in each request.
'strength' (int) Length of token that will be generate.
'persistentTokenMode' (bool) Persistent token mode. Set totrue
to not re-generate token every request which is good for ajax, set tofalse
(default) for re-generate every request.
__get()
Magic get.
public
__get(string $name) : mixed
Parameters
- $name : string
-
Property name.
Return values
mixed —Return its value depend on property.
createToken()
Create token.
public
createToken() : array<string|int, mixed>
Usage:
<?php $generateToken = $Csrf->createToken(); extract($generateToken); ?> <input type="hidden" name="<?php echo $csrfName; ?>" value="<?php echo $csrfKeyPair[$csrfName]; ?>"> <input type="hidden" name="<?php echo $csrfValue; ?>" value="<?php echo $csrfKeyPair[$csrfValue]; ?>">
Return values
array<string|int, mixed> —Return associative array with keys: 'csrfName', 'csrfValue', 'csrfKeyPair'.
getInstance()
Get CSRF class instance.
public
getInstance() : SlimGuard
Return values
SlimGuardgetTokenNameValueKey()
Get token name key and value key.
public
getTokenNameValueKey([bool $sequential = false ]) : array<string|int, mixed>
Parameters
- $sequential : bool = false
-
Set to
true
to return sequential array (indexed array),false
(default) to return associative array.
Return values
array<string|int, mixed> —Return sequential or associative array.
For sequential array (indexed array) first array is name key, second is value key.
For associative array it will return with keys 'csrfName', 'csrfValue'.
validateToken()
Validate CSRF token.
public
validateToken(string $name, string $value) : bool
Parameters
- $name : string
-
CSRF name.
- $value : string
-
CSRF token value.
Tags
Return values
bool —Return true
on success, false
on failure.