当前位置:Gxlcms > PHP教程 > php参数过滤、数据过滤示例详解

php参数过滤、数据过滤示例详解

时间:2021-07-01 10:21:17 帮助过:2人阅读

下面给大家介绍下PHP数据过滤

1、php提交数据过滤的基本原则

1)提交变量进数据库时,我们必须使用addslashes()进行过滤,像我们的注入问题,一个addslashes()也就搞定了。其实在涉及到变量取值时,intval()函数对字符串的过滤也是个不错的选择。
2)在php.ini中开启magic_quotes_gpc和magic_quotes_runtime。magic_quotes_gpc可以把get,post,cookie里的引号变为斜杠。magic_quotes_runtime对于进出数据库的数据可以起到格式话的作用。其实,早在以前注入很疯狂时,这个参数就很流行了。
3)在使用系统函数时,必须使用escapeshellarg(),escapeshellcmd()参数去过滤,这样你也就可以放心的使用系统函数。
4)对于跨站,strip_tags(),htmlspecialchars()两个参数都不错,对于用户提交的的带有html和php的标记都将进行转换。比如尖括号"<"就将转化为 "<"这样无害的字符。
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
strip_tags($text,);
5)对于相关函数的过滤,就像先前的include(),unlink,fopen()等等,只要你把你所要执行操作的变量指定好或者对相关字符过滤严密,我想这样也就无懈可击了。

2、PHP简单的数据过滤

1)入库: trim($str),addslashes($str)
2)出库: stripslashes($str)
3)显示: htmlspecialchars(nl2br($str))

下面通过一段代码给大家介绍php参数过滤

  1. class mysafe{
  2. public $logname;
  3. public $isshwomsg;
  4. function construct(){
  5. set_error_handler('MyError',E_ALL);
  6. //-----
  7. }
  8. function MyError($errno, $errstr, $errfile, $errline){
  9. echo "<b>Error number:</b> [$errno],error on line $errline in $errfile<br />";
  10. exit;
  11. }
  12. function wlog($logs){
  13. if(empty($logname)){
  14. $this->logname=$_SERVER["DOCUMENT_ROOT"]."/log.htm";
  15. }
  16. $Ts=fopen($this->logname,"a+");
  17. fputs($Ts,$logs."\r\n");
  18. fclose($Ts);
  19. }
  20. function showmsg($msg='',$flag=false){
  21. $this->isshwomsg=empty($this->isshwomsg) ? false : true;
  22. if ($this->isshwomsg) {
  23. echo '<br />--------------------------------------<br />';
  24. echo $msg;
  25. echo '<br />--------------------------------------<br />';
  26. if ($flag) exit;
  27. }
  28. }
  29. function get_filter(){
  30. $getfilter="'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)";
  31. foreach($_GET as $key=>$value){
  32. $this->StopAttack($key,$value,$getfilter);
  33. }
  34. }
  35. function post_filter(){
  36. $postfilter="\\b(and|or)\\b.{1,6}?(=|>|<|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)";
  37. foreach($_POST as $key=>$value){
  38. $this->StopAttack($key,$value,$postfilter);
  39. }
  40. }
  41. function cookie_filter(){
  42. $cookiefilter="\\b(and|or)\\b.{1,6}?(=|>|<|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)";
  43. foreach($_COOKIE as $key=>$value){
  44. $this->StopAttack($key,$value,$cookiefilter);
  45. }
  46. }
  47. //过滤参数
  48. function StopAttack($StrFiltKey,$StrFiltValue,$ArrFiltReq){
  49. if(is_array($StrFiltValue)){
  50. $StrFiltValue=implode($StrFiltValue);
  51. }
  52. if (preg_match("/".$ArrFiltReq."/is",$StrFiltValue)==1){
  53. $msg="<br><br>操作IP: ".$_SERVER["REMOTE_ADDR"]."<br>操作时间: ".strftime("%Y-%m-%d %H:%M:%S")."<br>操作页面:".$_SERVER["PHP_SELF"]."<br>提交方式: ".$_SERVER["REQUEST_METHOD"]."<br>提交参数: ".$StrFiltKey."<br>提交数据: ".$StrFiltValue;
  54. $this->wlog($msg);
  55. $this->showmsg($msg);
  56. exit();
  57. }
  58. }
  59. function filter_value_for_sql($str){
  60. $str = str_replace("and","",$str);
  61. $str = str_replace("execute","",$str);
  62. $str = str_replace("update","",$str);
  63. $str = str_replace("count","",$str);
  64. $str = str_replace("chr","",$str);
  65. $str = str_replace("mid","",$str);
  66. $str = str_replace("master","",$str);
  67. $str = str_replace("truncate","",$str);
  68. $str = str_replace("char","",$str);
  69. $str = str_replace("declare","",$str);
  70. $str = str_replace("select","",$str);
  71. $str = str_replace("create","",$str);
  72. $str = str_replace("delete","",$str);
  73. $str = str_replace("insert","",$str);
  74. $str = str_replace("'","",$str);
  75. $str = str_replace('"',"",$str);
  76. $str = str_replace(" ","",$str);
  77. $str = str_replace("or","",$str);
  78. $str = str_replace("=","",$str);
  79. $str = str_replace(" ","",$str);
  80. return $str;
  81. }
  82. //class end
  83. }

以上就是php 参数过滤、数据过滤示例详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行