Php PDO自定义类与安全性
我正在尝试创建自己的类,以便使用PDO与数据库一起工作/玩。我在课堂上采用了以下方法:Php PDO自定义类与安全性,php,mysql,oop,pdo,prepared-statement,Php,Mysql,Oop,Pdo,Prepared Statement,我正在尝试创建自己的类,以便使用PDO与数据库一起工作/玩。我在课堂上采用了以下方法: 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->
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);
}
}
在我的连接中有什么脆弱的东西吗?当我在我的类中提供其他CRUD方法时,如下所示:
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()
在此查询中,用户将使用以下方法以数组()的形式检索记录集,例如:
$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']);
}
$sno=1;
$user_name='%hussain%';
$aray=array(“:sno'=>$sno,”:user\u name'=>$user\u name);
foreach($crud->getRecordSet(“从用户中选择*,其中sno=:sno和用户名,如:user\u name“,$aray)作为$row){
echo(“
”.$row['user_name']);
echo(“
”.$row['user_password']);
回音(“
”.$row['date\u reg]”);
}
请让我知道,如果有什么问题,使我的类易受攻击
提前谢谢
Shah将功能更改为
public function getRecordSet($sql, $bindVars=array()){
$obj = $this->con->prepare($sql);
$obj->execute($bindVars);
return $obj->fetchAll();
}
然而,这是一个理智的问题,而不是安全问题。您以前是否有任何生产服务器在手,它们发送了一封关于数据库错误的电子邮件?是的,我有。但我仍然没有收到任何错误电子邮件…它运行了多长时间,流量是多少?在过去的一到三个月里,每天的流量是apx:70到80个用户一次。不,我在这个脚本中看不到任何用户。