for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* This file is part of the Shieldon package.
*
* (c) Terry L. <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* php version 7.1.0
* @category Web-security
* @package Shieldon
* @author Terry Lin <[email protected]>
* @copyright 2019 terrylinooo
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
* @link https://github.com/terrylinooo/shieldon
* @see https://shieldon.io
*/
declare(strict_types=1);
namespace Shieldon\Firewall\Panel;
use function count;
use function is_string;
/*
* Tradit for demonstration.
trait CsrfTrait
{
* See $this->csrf()
* @var array
protected $csrfField = [];
* Most popular PHP framework has a built-in CSRF protection such as Laravel.
* We need to pass the CSRF token for our form actions.
* @param string|array ...$csrfparams The arguments.
* @return void
public function csrf(...$csrfparams): void
foreach ($csrfparams as $value) {
foreach ($value as $k => $v) {
$this->csrfField[] = [
'name' => $k,
'value' => $v,
];
}
* Output HTML input element with CSRF token.
public function fieldCsrf(): void
if (!empty($this->csrfField)) {
foreach ($this->csrfField as $value) {
echo '<input type="hidden" name="' . $value['name'] . '" value="' . $value['value'] . '" id="csrf-field">';