## 數據過濾相關
### 安全過濾
~~~
/**
* thinksns_t函數用于過濾標簽,輸出沒有html的干凈的文本
* @param string text 文本內容
* @return string 處理后內容
*/
function thinksns_t($text){
$text = nl2br($text);
$text = real_strip_tags($text);
$text = addslashes($text);
$text = trim($text);
return $text;
}
/**
* thinksns_h 函數用于過濾不安全的html標簽,輸出安全的html
* @param string $text 待過濾的字符串
* @param string $type 保留的標簽格式
* @return string 處理后內容
*/
function thinksns_h($text, $type = 'html'){
// 無標簽格式
$text_tags = '';
//只保留鏈接
$link_tags = '<a>';
//只保留圖片
$image_tags = '<img>';
//只存在字體樣式
$font_tags = '<i><b><u><s><em><strong><font><big><small><sup><sub><bdo><h1><h2><h3><h4><h5><h6>';
//標題摘要基本格式
$base_tags = $font_tags.'<p><br><hr><a><img><map><area><pre><code><q><blockquote><acronym><cite><ins><del><center><strike>';
//兼容Form格式
$form_tags = $base_tags.'<form><input><textarea><button><select><optgroup><option><label><fieldset><legend>';
//內容等允許HTML的格式
$html_tags = $base_tags.'<meta><ul><ol><li><dl><dd><dt><table><caption><td><th><tr><thead><tbody><tfoot><col><colgroup><div><span><object><embed><param>';
//專題等全HTML格式
$all_tags = $form_tags.$html_tags.'<!DOCTYPE><html><head><title><body><base><basefont><script><noscript><applet><object><param><style><frame><frameset><noframes><iframe>';
//過濾標簽
$text = real_strip_tags($text, ${$type.'_tags'});
// 過濾攻擊代碼
if($type != 'all') {
// 過濾危險的屬性,如:過濾on事件lang js
while(preg_match('/(<[^><]+)(allowscriptaccess|ondblclick|onclick|onload|onerror|unload|onmouseover|onmouseup|onmouseout|onmousedown|onkeydown|onkeypress|onkeyup|onblur|onchange|onfocus|action|background|codebase|dynsrc|lowsrc)([^><]*)/i',$text,$mat)){
$text = str_ireplace($mat[0], $mat[1].$mat[3], $text);
}
while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
$text = str_ireplace($mat[0], $mat[1].$mat[3], $text);
}
}
return $text;
}
function real_strip_tags($str, $allowable_tags="") {
$str = html_entity_decode($str,ENT_QUOTES,'UTF-8');
return strip_tags($str, $allowable_tags);
}
~~~
### 在處理表單提交的時候我們可能會用上的驗證類
~~~
<?php
/**
* 驗證類
*/
class Verify{
/**
* 是否為空值
*/
public static function isEmpty($str){
$str = trim($str);
return !empty($str) ? true : false;
}
/**
* 數字驗證
* param:$flag : int是否是整數,float是否是浮點型
*/
public static function isNum($str,$flag = 'float'){
if(!self::isEmpty($str)) return false;
if(strtolower($flag) == 'int'){
return ((string)(int)$str === (string)$str) ? true : false;
}else{
return ((string)(float)$str === (string)$str) ? true : false;
}
}
/**
* 名稱匹配,如用戶名,目錄名等
* @param:string $str 要匹配的字符串
* @param:$chinese 是否支持中文,默認支持,如果是匹配文件名,建議關閉此項(false)
* @param:$charset 編碼(默認utf-8,支持gb2312)
*/
public static function isName($str,$chinese = true,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
if($chinese){
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u";
}else{
$match = '/^[A-za-z0-9_-]+$/';
}
return preg_match($match,$str) ? true : false;
}
/**
* 郵箱驗證
*/
public static function isEmail($str){
if(!self::isEmpty($str)) return false;
return preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$str) ? true : false;
}
//手機號碼驗證
public static function isMobile($str){
$exp = "/^1[3|4|5|7|8][0-9]{9}$/";
if(preg_match($exp,$str)){
return true;
}else{
return false;
}
}
/**
* URL驗證,純網址格式,不支持IP驗證
*/
public static function isUrl($str){
if(!self::isEmpty($str)) return false;
return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false;
}
/**
* 驗證中文
* @param:string $str 要匹配的字符串
* @param:$charset 編碼(默認utf-8,支持gb2312)
*/
public static function isChinese($str,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/"
: "/^[x{4e00}-x{9fa5}]+$/u";
return preg_match($match,$str) ? true : false;
}
/**
* UTF-8驗證
*/
public static function isUtf8($str){
if(!self::isEmpty($str)) return false;
return (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word)
== true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word)
== true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word)
== true) ? true : false;
}
/**
* 驗證長度
* @param: string $str
* @param: int $type(方式,默認min <= $str <= max)
* @param: int $min,最小值;$max,最大值;
* @param: string $charset 字符
*/
public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
$len = mb_strlen($str,$charset);
switch($type){
case 1: //只匹配最小值
return ($len >= $min) ? true : false;
break;
case 2: //只匹配最大值
return ($max >= $len) ? true : false;
break;
default: //min <= $str <= max
return (($min <= $len) && ($len <= $max)) ? true : false;
}
}
/**
* 驗證密碼
* @param string $value
* @param int $length
* @return boolean
*/
public static function isPWD($value,$minLen=6,$maxLen=16){
$match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';
$v = trim($value);
if(empty($v))
return false;
return preg_match($match,$v);
}
/**
* 驗證用戶名
* @param string $value
* @param int $length
* @return boolean
*/
public static function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
if(empty($value))
return false;
switch($charset){
case 'EN': $match = '/^[_\w\d]{'.$minLen.','.$maxLen.'}$/iu';
break;
case 'CN':$match = '/^[_\x{4e00}-\x{9fa5}\d]{'.$minLen.','.$maxLen.'}$/iu';
break;
default:$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
}
return preg_match($match,$value);
}
/**
* 驗證郵箱
* @param string $value
*/
public static function checkZip($str){
if(strlen($str)!=6){
return false;
}
if(substr($str,0,1)==0){
return false;
}
return true;
}
/**
* 匹配日期
* @param string $value
*/
public static function checkDate($str){
$dateArr = explode("-", $str);
if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
return true;
else
return false;
}
return false;
}
/**
* 匹配時間
* @param string $value
*/
public static function checkTime($str){
$timeArr = explode(":", $str);
if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
return true;
else
return false;
}
return false;
}
}
~~~
- 開始
- PHP配置參數的介紹
- PHP代碼優化
- php中的命名空間
- PHP文件上傳類
- PHP文件下載
- PHP驗證碼
- ThinkPHP3.2 框架函數
- A函數:實例化控制器
- C函數:設置和獲取配置參數
- D函數:實例化模型
- F 函數:快速緩存設置和存取
- M函數:例化模型(無需定義模型類)
- L函數:設置和獲取語言變量
- S 函數:緩存設置和存取
- R函數:直接調用控制器的操作方法
- U函數:URL地址生成
- I 函數:安全獲取系統輸入變量
- 日志
- ThinkPHP在關閉調試模式導致函數被緩存
- MySQL觸發器使用時遇到的坑
- PHP常用函數
- 五一回家記錄
- window的PHP開發(wamp)下安裝redis擴展
- Windows下安裝使用Redis
- PHP7新特性
- 利用 phpmailer 類實現隊列發送郵件
- GD 庫圖像處理
- 檢測 PHP 模塊是否開啟
- GD 庫操作一般步驟
- GD 庫繪畫改變字體
- GD 繪制驗證碼
- GD 縮略圖實現
- GD 繪制水印
- 日期時間函數庫
- PHP 函數
- 無限極分類