php常见验证
/**
* 文件上传
* @param $file 要上传的文件
* @param $size 大小设置
* @param $ext 文件类型
* @return bool 是否上传成功
*/
function imgUpload($file, $size, $ext){
$info = $file->validate(['size'=>$size,'ext'=>$ext])->move(ROOT_PATH . 'public/static/' . DS . 'uploads');
if($info){
$filename=$info->getSaveName();
$image=\think\Image::open("static/uploads/".$filename);
// 按照原图的比例生成一个最大为200*200的缩略图并保存为thumb.png
$image->thumb(200, 200)->save("static/uploads/".$filename);
return $filename;
}else{
return false;
}
}
/**
* 判断类型
* @param $num 需要判断是那种类型
*/
function judgeType($param,$type){
if($type=="array"&&is_array($param)){
return returnData(1,"success");
}else if($type=="string"&&is_string($param)){
return returnData(1,"success");
}else if($type=="int"&&is_int($param)){
return returnData(1,"success");
}else if($type=="float"&&is_float($param)){
return returnData(1,"success");
}
return returnData(0,"类型不正确");
}
/**
* 判断字段长度是否满足条件
* @param $str 要判断的字段
* @param $len 长度
*/
function judgeLen($str, $len){
$allLen=mb_strlen($str,'utf8');
if($allLen>$len){
return returnData(0,"长度过长");
}
return returnData(1,"");
}
/**
* 过滤危险字符
* @param $str
*/
function filterDangerChars($str){
foreach(config("dangerChars") as $item){
if(mb_strpos($str,$item,0,"utf8") !==false){
return returnData(0,"存在敏感字符");
break;
}
}
return returnData(1,"");
}
/**
* 基础过滤
* @param $param 字段
* @param $type 类型
* @param $len 长度
*/
function filterBase($param, $type, $len){
if(!isset($param)){
return returnData(0,"字段为空");
}
$typeResult=judgeType($param, $type); //判断类型是否一致
if($typeResult["code"]==0) return $typeResult;
$lenResult=judgeLen($param,$len); //判断长度是否满足要求
if($lenResult["code"]==0) return $lenResult;
$dangerResult=filterDangerChars($param); //是否包含非法字符
if($dangerResult["code"]==0) return $dangerResult;
return returnData(1,"success");
}
/**
* sql防注入 进行转义
* @param $param 需要过滤的字段
* @param bool|true $addslashes true---通过addslashes 转义,false---替换一些sql中的关键字
* @param string $type 转成什么类型
* @return int|mixed|string
*/
function filterSql($param, $addslashes=true, $type=""){
if($addslashes){
$param = addslashes($param);
}else{
$param=paramReplace($param);
}
if($type=="int"){
$param=changeType($param,"int");
}
return $param;
}
/**
* 强制转换成指定类型
* @param $param 需要转换的字段
* @param $type 需要转换的类型
* @return array|float|int|string 返回的数据
*/
function changeType($param, $type){
if($type=="int"){
$param = intval($param);
}else if($type="float"){
$param = floatval($param);
}else if($type=="string"){
$param = strval($param);
}else if($type=="array"){
$param=array($param);
}
return $param;
}
/**
* 过滤一些特殊的sql语句中的关键字
* @param $str
* @return mixed
*/
function paramReplace($str)
{
$str = str_replace(" ","",$str);
$str = str_replace("\n","",$str);
$str = str_replace("\r","",$str);
$str = str_replace("'","",$str);
$str = str_replace('"',"",$str);
$str = str_replace("or","",$str);
$str = str_replace("and","",$str);
$str = str_replace("#","",$str);
$str = str_replace("\\","",$str);
$str = str_replace("null","",$str);
$str = str_replace(">","",$str);
$str = str_replace("<","",$str);
$str = str_replace("=","",$str);
$str = str_replace("char","",$str);
$str = str_replace("order","",$str);
$str = str_replace("select","",$str);
$str = str_replace("create","",$str);
$str = str_replace("delete","",$str);
$str = str_replace("insert","",$str);
$str = str_replace("execute","",$str);
$str = str_replace("update","",$str);
$str = str_replace("count","",$str);
return $str;
}
/**
* 为了避免xss漏洞 把指定的标签进行过滤 (strip_tags 去掉全部)
* @param $param 要过滤的字段 $str="dfdf<b>dfdf</b><script>alert('1111');<em>斜体</em></script>";
* @param $tags 指定哪些标签需要过滤
* @return mixed
*/
function filterXSS($param, $tags){
foreach($tags as $tag) {
$pattern = '/\<.*?'.$tag.'.*?\>.*\<(\/)?'.$tag.'.*?\>/i';
//正则过滤指定标签
$param= preg_replace($pattern, '', $param);
}
return $param;
}
/**
* 为了避免xss漏洞,把标签转为字符串
* @param $param 需要把标签转成字符串的的字段
*/
function filterXSS1($param){
$param = htmlspecialchars($param);
return $param;
}
/**
*
*/
function filterUpload($file,$type){
if($type=="img"){
if(in_array($file,config("img"))){
return returnData(1,"");
}
}
return returnData(0,"类型不正确");
}
/**
* @param $code 0---error 1-success
* @param $data
* @return array 返回的是数组
*/
function returnData($code, $data){
return array(
"code"=>$code,
"data"=>$data
);
}
php常见验证的更多相关文章
- iis配置asp.net常见验证失败问题解决方案
很多朋友在用IIS6架网站的时候遇到不少问题,而这些问题有些在过去的IIS5里面就遇到过,有些是新出来的,俺忙活了一下午,做 了很多次试验,结合以前的排错经验,做出了这个总结,希望能给大家帮上忙:) ...
- c# 常见验证邮箱、电话号码、日期等格式
#region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...
- java常见验证邮箱、电话号码、日期等格式
package besttone.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 90%的验证 ...
- JS的一些常见验证代码
1//檢查空串 2function isEmpty(str){ 3 if((str == null)||(str.length == 0)) return (true); 4 else retu ...
- verify.js使用验证插件使用
github:https://github.com/52fhy/verify.js 首先引入js,最好拷贝verify整个目录,因为里面有图标. <script src="verify ...
- 表单验证插件 jquery.validata 使用方法
参考资料:http://www.runoob.com/jquery/jquery-plugin-validate.html 下载地址 jquery.validate插件的文档地址http://docs ...
- 开始VS 2012 中LightSwitch系列的第1部分:表中有什么?描述你的数据
[原文发表地址] Beginning LightSwitch in VS 2012 Part 1: What’s in a Table? Describing Your Data [原文发表时间] ...
- 开始VS 2012中LightSwitch系列的第5部分:我可以使用用户权限来控制访问权吗?
[原文发表地址] Beginning LightSwitch in VS 2012 Part 5: May I? Controlling Access with User Permissions [ ...
- .net 常用正则表达式
Net中正则表达式的简单使用方法及常见验证判断 判断字符串是只是数字我们可以这样写:return new System.Text.RegularExpressions.Regex(@"^([ ...
随机推荐
- 洛谷P1976 鸡蛋饼(Catalan数)
P1976 鸡蛋饼 题目背景 Czyzoiers 都想知道小 x 为什么对鸡蛋饼情有独钟.经过一番逼问,小 x 道出 了实情:因为他喜欢圆. 题目描述 最近小 x 又发现了一个关于圆的有趣的问题:在圆 ...
- 数学补天 By cellur925
质数 bool prime(int q) { ||q==) ; ) ; !=||q%!=) ; int cnt=sqrt(q); ;i<=cnt;i+=) !=||q%(i+)!=) ; ; } ...
- SpringMVC入门 bug集锦X2
package cn.itcast.converter; import org.springframework.core.convert.converter.Converter; import jav ...
- 链家H5项目总结
在此次项目中,使用的是高度百分比.对于适配这一块确实少了很多. 1.如果是用高度百分比的话.则img需要写成这样的样式. img{ width:auto; height:100%; display: ...
- iphone状态栏,导航栏,标签栏高度一览表
iphone状态栏,导航栏,标签栏高度一览表 设备分辨率 状态栏高度 导航栏高度 标签栏高度 iPhone6 plus 1242×2208 px 60px 132px 147px iPhon ...
- ES6新特性使用小结(一)
一.let const 命令 'use strict'; /*function test(){ //let a = 1; for(let i=1;i<3;i++){ console.log(i) ...
- BZOJ 1047: [HAOI2007]理想的正方形 单调队列瞎搞
题意很简明吧? 枚举的矩形下边界和右端点即右下角,来确定矩形位置: 每一个纵列开一个单调队列,记录从 i-n+1 行到 i 行每列的最大值和最小值,矩形下边界向下推移的时候维护一下: 然后在记录的每一 ...
- 题解 P1004 方格取数
传送门 动态规划Yes? 设i为路径长度,(为什么i这一维可以省掉见下)f[j][k]表示第一个点到了(j,i-j),第二个点到了(k,j-k) 则 int ji=i-j,ki=i-k; f[j][k ...
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) B
Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he we ...
- Consul实现服务治理1
NET Core微服务之基于Consul实现服务治理 https://www.cnblogs.com/edisonchou/p/9148034.html 一.Consul服务注册之配置文件方式 1.1 ...