php 字段验证类库,PHP验证类库常用数据安全验证
我們知道框架里面基本都會包含有最基本的安全驗證,那么平時在做項目中,不一定都是用到的是某種框架,可能是某種自定義的方法,隨著自己的業務編寫出來的一種MVC模式框架,那么很多類庫以及方法都是在業務中不斷去完善。所以以下這個驗證類,當你用的不是某種框架自帶的安全驗證,也可以參照以下的方式,作為一些數據的基本驗證。
驗證類如下:
class Verify{
public $msg = '';
/**
* 是否為空值
*/
public function isEmpty($str){
$str = trim($str);
$res = empty($str) ? true : false;
$this->msg = $res ? '不能為空!' : '';
return $res;
}
/**
* 數字驗證
* param:$flag : int是否是整數,float是否是浮點型
*/
public function isNum($str,$flag = 'float'){
if($this->isEmpty($str)) return false;
if(strtolower($flag) == 'int'){
$res = ((string)(int)$str === (string)$str) ? true : false;
}else{
$res = ((string)(float)$str === (string)$str) ? true : false;
}
$this->msg = $res ? '' : $str.'不是數字!';
return $res;
}
/**
* 名稱匹配,如用戶名,目錄名等
* @param:string $str 要匹配的字符串
* @param:$chinese 是否支持中文,默認支持,如果是匹配文件名,建議關閉此項(false)
* @param:$charset 編碼(默認utf-8,支持gb2312)
*/
public function isName($str,$chinese = true,$charset = 'utf-8'){
if($this->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_-]+$/';
}
$res = preg_match($match,$str) ? true : false;
$this->msg = $res ? '' : $str.'不是一個合法的名字';
return $res;
}
/**
* 郵箱驗證
*/
public function isEmail($str){
if($this->isEmpty($str)) return false;
$res = 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;
$this->msg = $res ? '' : $str.'不是合法郵箱!';
return $res;
}
//手機號碼驗證
public function isMobile($str){
if($this->isEmpty($str)) return false;
$exp = "/^1[3|4|5|7|8][0-9]{9}$/";
if(preg_match($exp,$str)){
$res = true;
}else{
$res = false;
}
$this->msg = $res ? '' : $str.'手機號碼格式不對!';
return $res;
}
/**
* URL驗證,純網址格式,不支持IP驗證
*/
public function isUrl($str){
if($this->isEmpty($str)) return false;
$res = preg_match('#(http://|https://)?(www.)?(\w+\.)+\w+#i',$str) ? true : false;
$this->msg = $res ? '' : $str.'URL格式不對!';
return $res;
}
/**
* 驗證中文
* @param:string $str 要匹配的字符串
* @param:$charset 編碼(默認utf-8,支持gb2312)
*/
public function isChinese($str,$charset = 'utf-8'){
if($this->isEmpty($str)) return false;
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/" : "/^[x{4e00}-x{9fa5}]+$/u";
$res = preg_match($match,$str) ? true : false;
$this->msg = $res ? '' : $str.'不是一個中文!';
return $res;
}
/**
* UTF-8驗證
*/
public function isUtf8($str){
if($this->isEmpty($str)) return false;
$res = (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;
$this->msg = $res ? '' : $str.'不是uft8編碼!';
return $res;
}
/**
* 驗證長度
* @param: string $str
* @param: int $type(方式,默認min <= $str <= max)
* @param: int $min,最小值;$max,最大值;
* @param: string $charset 字符
*/
public function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){
if($this->isEmpty($str)) return false;
$len = mb_strlen($str,$charset);
switch($type){
case 1: //只匹配最小值
$res = ($len >= $min) ? true : false;
$this->msg = $res ? '' : "{$str}長度最小必須{$min}個字符";
break;
case 2: //只匹配最大值
$res = ($max >= $len) ? true : false;
$this->msg = $res ? '' : "{$str}長度最大必須{$max}個字符";
break;
default: //min <= $str <= max
$res = (($min <= $len) && ($len <= $max)) ? true : false;
$this->msg = $res ? '' : "{$str}長度必須在{$min}到{$max}個字符之間";
}
return $res;
}
/**
* 驗證用戶名
* @param string $value
* @param int $length
* @$res = boolean
*/
public function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
if($this->isEmpty($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';
}
$res = preg_match($match,$value);
$this->msg = $res ? '' : $value.'不是一個合理的用戶名!';
return $res;
}
/**
* 匹配日期
* @param string $value
*/
public function checkDate($str){
if($this->isEmpty($str)) return false;
$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))
$res = true;
else
$res = false;
}else{
$res = false;
}
$this->msg = $res ? '' : $str.'不是一個合理的日期格式!';
return $res;
}
/**
* 匹配時間
* @param string $value
*/
public function checkTime($str){
if($this->isEmpty($str)) return false;
$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))
$res = true;
else
$res = false;
}else{
$res = false;
}
$this->msg = $res ? '' : $str.'不是一個合理的時間格式!';
return $res;
}
public function getError(){
return $this->msg;
}
}
此驗證類庫支持驗證用戶名、數字、郵箱、url、中文、長度、密碼、日期和時間等,你可以很方便的應用到表單提交,作為數據安全驗證。以上這么驗證規則,一樣可以照搬到js中最為前端的驗證
使用方式如下:
$verify = new Verify();
$mobile = '1008611';
if(!$verify->isMobile($mobile)){
die($verify->getError());
}else{
echo '正確';
}
總結
以上是生活随笔為你收集整理的php 字段验证类库,PHP验证类库常用数据安全验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音怎么剪辑视频
- 下一篇: 峰米激光电视连续 6 年双 11 线上销