• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP防注入安全代码_php技巧

php 搞代码 4年前 (2022-01-26) 16次浏览 已收录 0个评论

简述:/*************************
说明:
判断传递的变量中是否含有非法字符
如$_POST、$_GET
功能:防注入
**************************/

<?php      <br><br>//要过滤的非法字符      <BR>$ArrFiltrate=array("'",";","union");      <BR>//出错后要跳转的url,不填则默认前一页      <BR>$StrGoUrl="";      <BR>//是否存在数组中的值      <BR>function FunStringExist($StrFiltrate,$ArrFiltrate){      <BR>foreach ($ArrFiltrate as $key=>$value){      <BR>  if (eregi($value,$StrFiltrate)){      <BR>    return true;      <BR>  }      <BR>}      <BR>return false;      <BR>}      <br><br>//合并$_POST 和 $_GET      <BR>if(function_exists(array_merge)){      <BR>  $ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);      <BR>}else{      <BR>  foreach($HTTP_POST_VARS as $key=>$value){      <BR>    $ArrPostAndGet[]=$value;      <BR>  }      <BR>  foreach($HTTP_GET_VARS as $key=>$value){      <BR>    $ArrPostAndGet[]=$value;      <BR>  }      <BR>}      <br><br>//验证开始      <BR>foreach($ArrPostAndGet as $key=>$value){      <BR>  if (FunStringExist($value,$ArrFiltrate)){      <BR>    echo "<script language=\"javascript\">alert(\"非法字符\");</script>";      <BR>    if (emptyempty($StrGoUrl)){      <BR>    echo "<script language=\"javascript\">history.go(-1);</script>";      <BR>    }else{      <BR>    echo "<script language=\"javascript\">window.location=\"".$StrGoUrl."\";</script>";      <BR>    }      <BR>    exit;      <BR>  }      <BR>}      <BR>?> <BR>

保存为checkpostandget.php
然后在每个php文件前加include(“checkpostandget.php“);即可

方法2

/* 过滤所有GET过来变量 */     <BR>foreach ($_GET as $get_key=>$get_var)      <BR>{      <BR>if (is_numeric($get_var)) {      <BR>  $get[strtolower($get_key)] = get_int($get_var);      <BR>} else {      <BR>  $get[strtolower($get_key)] = get_str($get_var);      <BR>}      <BR>}      <br><br>/* 过滤所有POST过来的变量 */     <BR>foreach ($_POST as $post_key=>$post_var)      <BR>{      <BR>if (is_numeric($post_var)) {      <BR>  $post[strtolower($post_key)] = get_int($post_var);      <BR>} else {      <BR>  $post[strtolower($post_key)] = get_str($post_var);      <BR>}  <i>·本2文来源gaodai$ma#com搞$代*码网2</i><strong>搞gaodaima代码</strong>    <BR>}      <br><br>/* 过滤函数 */     <BR>//整型过滤函数      <BR>function get_int($number)      <BR>{      <BR>    return intval($number);      <BR>}      <BR>//字符串型过滤函数      <BR>function get_str($string)      <BR>{      <BR>    if (!get_magic_quotes_gpc()) {      <BR>return addslashes($string);      <BR>    }      <BR>    return $string;      <BR>}<BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP防注入安全代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址