定制 supergnaw/form-security 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

supergnaw/form-security

最新稳定版本:v1.0.2

Composer 安装命令:

composer require supergnaw/form-security

包简介

Class for simplifying webpage form security

README 文档

README

PHP class for simplifying webpage form security. The main purpose for the creation of this class was to help mitigate against CSRF. The class can validate the following types of inputs:

  • boolean
  • numeric
    • float
    • integer
    • hexadecimal integer
    • octal integer
  • network
    • ipv4
    • ipv6
    • mac address
  • timestamp
    • date
    • time
    • timestamp
  • string
    • alphanumeric
    • hexadecimal
    • email
    • encoded html
    • url
    • string

Token

Form Tokens

FormSecurity can generate nonce tokens to be used to verify a form can only be submitted once. To use the token, simply generate a token, add it to a hidden form input, then validate the submitted token against the one saved in the session variable.

Generate the token

$exampleToken = FormSecurity::token_generate('example');

Store the generated token in a form hidden input

<form>
    <input type="hidden" name="token_name" value="<?php echo $exampleToken; ?>">
</form>

Validate the submitted token

if (FormSecurity::token_verify("token_name")) {
    echo "security token is valid!";
} else {
    echo "security token is invalid!";
}

Clearing All Tokens

If for whatever reason you need to clear any saved tokens, use token_clear_all().

FormSecurity::token_clear_all();

Whitelist & Blacklist

FormSecurity can apply a whitelist or blacklist to a given input and filter out unwanted or unexpected inputs.

$input = [
    "var1" => 1,
    "var2" => 2,
    "var3" => 3
];

$whitelist = ["var1", "var2"];
$output = FilterSecurity::apply_whitelist($whitelist, $input);
// $output = [1, 2]

$blacklist = ["var1", "var3"];
$output = FilterSecurity::apply_blacklist($blacklist, $input);
// $output = [2]

Filter

FormSecurity can filter the values of a given input (get, post, cookie, server, or env) and remove any values that do not match their expected type (bool, float, hexint, int, octint, ipv4, ipv6, mac, date, time , timestamp, string, alnum, url, email, or htmlenc).

$filter = [
    "foo" => "int",
    "bar" => "string",
];

$_GET = [
    "foo" => "1",
    "bar" => "2"
]
$get = FormSecurity::filter_input(input: "get", types: $filter);
// $get = ["2"]

$_POST = [
    "foo" => 1,
    "bar" => 2
];
$post = FormSecurity::filter_input(input: "post", types: $filter);
// $post = [1]

Granular Filtering

FormSecurity filters can also be used individually to validate individual inputs, where null will be returned on any failure.

  • FormSecurity::filter_boolean()
  • FormSecurity::filter_float()
  • FormSecurity::filter_int()
  • FormSecurity::filter_hexint()
  • FormSecurity::filter_octint()
  • FormSecurity::filter_ipv4()
  • FormSecurity::filter_ipv6()
  • FormSecurity::filter_mac()
  • FormSecurity::filter_string()
  • FormSecurity::filter_alnum()
  • FormSecurity::filter_hex()
  • FormSecurity::filter_url()
  • FormSecurity::filter_email()
  • FormSecurity::filter_htmlenc()
  • FormSecurity::filter_date()
  • FormSecurity::filter_time()
  • FormSecurity::filter_timestamp()

String Cleaning

FormSecurity can clean strings

Allow only alphabetical characters

$input = "Hell0 w0rld!";
$output = FormSecurity::clean_to_alpha($input);
// $output = "Hellwrld"

Allow only alphanumeric characters

$input = "Hell0 w0rld!";
$output = FormSecurity::clean_to_alnum($input);
// $output = "Hell0w0rld"

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: AGPL-3.0
  • 更新时间: 2024-03-20