我的开发环境:
mysql5.0,  php5.2.6, apache2.2.8对于表单提交,客户端网址传递等如何防范?我现在简单的通过PHP程序向表中插入一条记录是可以的。一但我对客户端传过来的参数使用了mysql_real_escape_string()函数,程序就报错,说连不上数据库。是不是使用mysql_real_escape_string()函数,先要设置PHP.INI中某个变量?下面贴出部下代码
/*
 获取客户端提交的表单
*/
$strseriesid=qstr($_POST["seriesid"], get_magic_quotes_gpc());
$strseriesname=qstr($_POST["seriesname"], get_magic_quotes_gpc());qstr函数如下:
function qstr($string, $magic_quotes=false, $tag=false)
{
  $tag_str = '';
  if ($tag) $tag_str = "'";
  if (!$magic_quotes) {
    if (strnatcmp(PHP_VERSION, '4.3.0') >= 0) {
      return $tag_str.mysql_real_escape_string($string).$tag_str;
    }
    $string = str_replace("'", "\\'" , str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string)));
    return  $tag_str.$string.$tag_str;
  }
  return $tag_str.str_replace('\\"', '"', $string).$tag_str; 
 
}第一次开发PHP项目,请高手指点一下,谢谢!

解决方案 »

  1.   

    $strseriesid=qstr($_POST["seriesid"], get_magic_quotes_gpc());
    $strseriesname=qstr($_POST["seriesname"], get_magic_quotes_gpc());
    2点:一就是你的这两个变量在接受了客户端的数据时:对其接受的变量进行过滤出现一些可以字符则替换:就可以了;
    二:是你的SQL 语句要写的完整:
    function inject_check($sql_str){
        $check = eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile',$sql_str);    //排除过滤
        if($check){
            echo "您输入了非法注入内容";        //如果用人注入则提示
            exit;
        }else{
            return $sql_str;
        }
    基本就是这样的:思路
      

  2.   

    string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] )使用 string mysql_real_escape_string 函数的条件是先连接 mysql 数据库
      

  3.   

    看了两位回答我还是不太清楚如何使用string mysql_real_escape_string我贴出添加时的全部代码,代码可能有点乱
    <?php
    include 'mysql.class.php';
    include 'func.php';  $strseriesid=qstr($_POST["seriesid"], get_magic_quotes_gpc()); //qstr函数就在func.php中,具体代码可看我发的贴子.
      $strseriesname=qstr($_POST["seriesname"], get_magic_quotes_gpc());
      这里省略掉其它表单数据,说明一下,如果我的表单变量是$strseriesid=$_POST["seriesid"],程序不会错
      
      $strsql="insert into photoseries(seriesid,seriesname,brandid,photoid,smallphotoid) values('$strseriesid','$strseriesname','$strbrandid','$strphotoid','$strsmallphotoid')";  
     
    $rs=new dbmysql();

    $rs->query($strsql);
    ?>
      

  4.   

    推荐你用一些现成的 db 类库 比如你去下载 wordpress 的代码 然后用里面的 wp_db.php 就不错 当然 其他的 也都可以解决这个问题 
      

  5.   


    wordpress 是不是很复杂,下载下来看不懂。是不是不适合我这种菜鸟学习?
      

  6.   

    我现在怀疑是不是$strseriesname=qstr($_POST["seriesname"], get_magic_quotes_gpc()) get_magic_quotes_gpc()这个参数传的不对?使用其它参数?