给一个可以基本防止SQL注入的例程:function _prepare_query($args)
{
    $sql = array_shift($args);
    if (count($args)) {
        $pieces = explode('?', $sql);
        if (count($pieces) != count($args) + 1)
            die('System error: query syntax');        $strip = get_magic_quotes_gpc();
        $i = 0;
        foreach ($args as $arg) {
            if (is_null($arg) || empty($arg))
                $arg = "''";
            else if (is_scalar($arg)) {
                if (is_string($arg)) {
                    if ($strip)
                        $arg = stripslashes($arg);
                    $arg = "'" . $arg . "'";
                }
            } else
                die('System error: query parameter');
            $pieces[$i++] .= $arg;
        }        $sql = implode('', $pieces);
    }
    return $sql;
}function do_query()
{
    $args = func_get_args();
    $sql = _prepare_query($args);
    return ibase_query($sql);
}使用时这样:
do_query("select * from table1 where id=? and datein=? orderby id", $id, $aDate);

解决方案 »

  1.   

    BTW,这个是基于InterBase的,如果用MySQL,只要将ibase_query换成mysql_query即可。
      

  2.   

    安全这东西不是几个函数就能搞定的...sql只是最常见的...就是菜鸟级别黑客会用的那种...session/include/form/file的安全问题都大了去了,更不要提虚拟主机的问题...拿shell很随意所以,楼主加油学吧...系统的看看essential php security
      

  3.   

    有本书叫做essential php security 很不错推荐看看有中文版的