deftx / gump
此包最新版本(dev-master)没有可用的许可证信息。
Wixel/GUMP 的 Composer 包
dev-master
2013-05-17 13:57 UTC
Requires
- php: >=5.2.0
This package is not auto-updated.
Last update: 2024-09-23 13:01:08 UTC
README
GUMP 是一个独立的 PHP 输入验证和过滤类。
- 下载 GUMP
- 解压并将其目录复制到您的 PHP 项目目录中。
在您的项目中包含它
require "gump.class.php";
可用方法
validation_rules(array $rules); // Get or set the validation rules filter_rules(array $rules); // Get or set the filtering rules run(array $data); // Runs the filter and validation routines xss_clean(array $data); // Strips and encodes unwanted characters sanitize(array $input, $fields = NULL); // Sanitizes data and converts strings to UTF-8 (if available) validate(array $input, array $ruleset); // Validates input data according to the provided ruleset (see example) filter(array $input, array $filterset); // Filters input data according to the provided filterset (see example) get_readable_errors($convert_to_string = false); // Returns human readable error text in an array or string
完整示例
以下示例是注册表单的一部分,流程应该是相当标准的
# Note that filters and validators are separate rule sets and method calls. There is a good reason for this. require "gump.class.php"; $gump = new GUMP(); $_POST = $gump->sanitize($_POST); // You don't have to sanitize, but it's safest to do so. $gump->validation_rules(array( 'username' => 'required|alpha_numeric|max_len,100|min_len,6', 'password' => 'required|max_len,100|min_len,6', 'email' => 'required|valid_email', 'gender' => 'required|exact_len,1|contains,m f', 'credit_card' => 'required|trim|valid_cc' )); $gump->filter_rules(array( 'username' => 'trim|sanitize_string|mysql_escape', 'password' => 'trim|base64', 'email' => 'trim|sanitize_email', 'gender' => 'trim', 'bio' => 'noise_words' )); $validated_data = $gump->run($_POST); if($validated_data === false) { echo $gump->get_readable_errors(true); } else { print_r($validated_data); // validation successful }
返回值
run()
返回两种类型之一
数组 包含成功验证和过滤的数据(当验证成功时),或返回 FALSE(当验证失败时)
validate()
返回两种类型之一
数组 包含键名和验证器名(当数据未通过验证时)。
您可以使用此数组与您的语言助手一起确定要显示的错误消息。
布尔值 为 TRUE,如果验证成功。
filter()
返回解析为 $input
参数的精确数组结构,唯一的区别是过滤后的数据。
可用验证器
- required
确保指定的键值存在且不为空
- valid_email
检查是否为有效的电子邮件地址
- max_len,n
检查键值长度,确保其长度不超过指定的长度。n = 长度参数。
- min_len,n
检查键值长度,确保其长度不短于指定的长度。n = 长度参数。
- exact_len,n
确保键值长度与指定的长度精确匹配。n = 长度参数。
- alpha
确保键值中只包含字母字符(a-z,A-Z)
- alpha_numeric
确保键值中只包含字母数字字符(a-z,A-Z,0-9)
- alpha_dash
确保键值中只包含字母数字字符、破折号和下划线(a-z,A-Z,0-9,_-)
- numeric
确保键值只包含数字
- integer
确保键值只包含整数
- boolean
检查是否为 PHP 接受的布尔值,当值为 "1"、"true"、"on" 或 "yes" 时返回 TRUE
- float
检查是否为浮点值
- valid_url
检查是否为有效的 URL 或子域
- url_exists
检查 URL 是否存在且可访问
- valid_ip
检查是否为有效的通用 IP 地址
- valid_ipv4
检查是否为有效的 IPv4 地址
- valid_ipv6
检查是否为有效的 IPv6 地址
- valid_cc
检查是否为有效的信用卡号(使用 MOD10 校验和算法)
- valid_name
检查是否为有效的人名格式
- contains,n
验证值是否包含在预定义的值集中
可用过滤器
过滤器可以是任何返回字符串的 PHP 函数。如果存在执行您想要过滤器执行的操作的 PHP 函数,则无需创建自己的函数。
- sanitize_string
删除脚本标签并编码 HTML 实体,类似于 GUMP::xss_clean();
- urlencode
编码 URL 实体
- htmlencode
编码 HTML 实体
- sanitize_email
从电子邮件地址中删除非法字符
- sanitize_numbers
删除任何非数字字符
- trim
删除字符串开头或结尾的空格
- base64_encode
对输入进行Base64编码
- base64_decode
对输入进行Base64解码
- sha1
使用安全的sha1算法对输入进行加密
- md5
对输入进行MD5编码
- noise_words
从字符串中移除噪声词汇
- json_encode
创建输入的JSON表示
- json_decode
解码JSON字符串
- rmpunctuation
从字符串中移除所有已知的标点符号
- basic_tags
从文本中移除所有布局方向的HTML标签。仅保留基本标签
创建自己的验证器和过滤器
简单地创建一个扩展GUMP类的自定义类。
require("gump.class.php"); class MyClass extends GUMP { public function filter_myfilter($value) { ... } public function validate_myvalidator($field, $input, $param = NULL) { ... } } // EOC $validator = new MyClass(); $validated = $validator->validate($_POST, $rules);
请记住创建具有正确参数类型和数量的公共方法。
对于过滤器方法,在方法名前加"filter_"。对于验证器方法,在方法名前加"validate_"。
运行示例
- 打开您的终端
- cd [GUMP 目录/示例]
- php [文件].php
输出将取决于输入数据。
待办事项
- 添加composer兼容性
- 货币验证器
- 地址验证器
- 国家验证器
- 位置坐标验证器
- HTML验证器
- 语言验证 ... 确定文本是否为指定的语言
- 验证垃圾邮件域名或IP。
- 验证垃圾邮件电子邮件地址
- 使用askimet或类似工具验证垃圾邮件文本
- 改进文档
- 更多示例
- W3C验证过滤器?
- 一个与HTML tidy服务集成的过滤器: http://infohound.net/tidy/
- 添加twitter & facebook个人资料URL验证器: http://stackoverflow.com/questions/2845243/check-if-twitter-username-exists
- 添加更多逻辑示例 - 登录表单、个人资料更新表单、博客文章表单等。