doufei3152 2014-03-19 13:39
浏览 47
已采纳

PDO自定义类和安全性

I am trying to create my own class to work/play with database with PDO. I am having the following method in my class:

private function connect(){
    try{
        $this->con = new PDO("mysql:host={$this->host};dbname={$this->db_name};charset=utf8", $this->db_user, $this->db_pass); 
        $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        $this->con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        $this->con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }catch(PDOException $ex){
        $this->catchError($ex);
        }
    }

is there any thing vulnerable in my connection? while i am providing other CRUD method in my class like this:

public function getRecordSet($sql,$bindVars=array()){
            $ary = array();
            try{
                $this->connect();
                $obj = $this->con->prepare($sql);
                if(count($bindVars) > 0){
                    $obj->execute($bindVars);
                    }
                else{
                    $obj->execute();
                    }
                $ary = $obj->fetchAll();
            }catch(PDOException $ex){
                $this->catchError($ex); //Production Server: send exception through email 
                //echo($ex->getMessage()); //Developer Machine: Display Exceptions in browser
                }
                $this->con = null;
                return $ary;
            }//getRecordSet()

In this query users will retrive recordset as an array() using the following way for example:

        $sno = 1;
        $user_name = '%hussain%';
        $aray = array(':sno'=>$sno,':user_name'=>$user_name);
        foreach($crud->getRecordSet("SELECT * FROM users WHERE sno = :sno AND user_name LIKE :user_name",$aray) as $row){
            echo('<br>'.$row['user_name']);
            echo('<br>'.$row['user_password']);
            echo('<br>'.$row['date_reg']);
            }

Please let me know if there is anything goes wrong and makes my class vulnerable?

Thanks in advance.

Shah

  • 写回答

1条回答 默认 最新

  • dongliyu3278 2014-03-19 13:56
    关注

    Change function to

    public function getRecordSet($sql, $bindVars=array()){
        $obj = $this->con->prepare($sql);
        $obj->execute($bindVars);
        return $obj->fetchAll();
    }
    

    however, it's rather matter of sanity, not security

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导