搜索
 找回密码
 加入

PHP反SQL注入的函数!

ctgwglzc 2009-9-25 13:04:35 1357
  1. function log_attack($type, $query) //Created By Heretic
  2. {
  3.      $agent = $_SERVER['HTTP_USER_AGENT'];
  4.      $uri = $_SERVER['REQUEST_URI'];
  5.      $ip = $_SERVER['REMOTE_ADDR'];
  6.      $ref = $_SERVER['HTTP_REFERER'];
  7.      $dtime = date('r');
  8.       
  9.      if($ref == ""){
  10.          $ref = "None";
  11.      }
  12.      if($user == ""){
  13.          $user = "None";
  14.      }
  15.      $location = "security/";
  16.      $type = $location . $type . ".html";
  17.      $entry_line = "$dtime - IP: $ip | Agent: $agent  | URL: $uri | Referrer: $ref | Query : $query <BR>\n";
  18.      $fp = fopen("$type", "a");
  19.      fputs($fp, $entry_line);
  20.      fclose($fp);  
  21. }



  22. function test($string)
  23. {

  24. $string2 = preg_replace( '/[^A-Za-z0-9@\.\-_\+ ]*/', '', $string );
  25. $string2= strip_tags($string2);
  26. if (($string) <> ($string2)){
  27. log_attack('sql_attack', $string);

  28. }


  29. return $string2;
  30. }
复制代码

3 回复

talkweb
2009-9-24 18:56:29
点击查看详情
我有.NET的反SQL注入

                #region 输入用户名和密码时进行处理,清除SQL注入式攻击(来自Microsoft)
                /// <summary>
                /// 输入用户名和密码时进行处理,清除SQL注入式攻击(来自Microsoft)
                /// </summary>
                /// <param name="inputString">输入值</param>
                /// <param name="maxLength">取输入值的多长</param>
                /// <returns>处理后的值</returns>
                public static string InputText(string inputString, int maxLength)
                {                       
                        StringBuilder retVal = new StringBuilder();
                       
                        if ((inputString != null) && (inputString != String.Empty))
                        {
                                inputString = inputString.Trim();
                                if (inputString.Length > maxLength)
                                        inputString = inputString.Substring(0, maxLength);                               
                                for (int i = 0; i < inputString.Length; i++)
                                {
                                        switch (inputString[i])
                                        {
                                                case '"':
                                                        retVal.Append("&quot;");
                                                        break;
                                                case '<':
                                                        retVal.Append("&lt;");
                                                        break;
                                                case '>':
                                                        retVal.Append("&gt;");
                                                        break;
                                                default:
                                                        retVal.Append(inputString[i]);
                                                        break;
                                        }
                                }
                               
                                retVal.Replace("'", " ");
                        }
                        String sRetrun =retVal.ToString();
                        sRetrun=sRetrun.Replace("--","");
                        return sRetrun.ToString().Trim();                       
                }
                #endregion
ww34856950
2009-9-25 13:04:24
我还是看不懂
ww34856950
2009-9-25 13:04:35
能说的清楚一点有什么用吗
高级模式
游客