【问题标题】:Prevent SQL Injection in CodeIgniter防止 CodeIgniter 中的 SQL 注入
【发布时间】:2016-03-22 05:12:45
【问题描述】:

CI Active Record 能防止 SQL 注入吗?

这是我的代码

    $this->db->select('t.DateTime,
                       su.FirstName,
                       su.MiddleName,
                       su.LastName,
                       l.Description,
                       t.Temperature,
                       t.UnitID,
                       t.Humidity');
    $this->db->from('temperaturelogs as t');
    $this->db->join('systemusers as su', 'su.UserID = t.Encoder');
    $this->db->join('locations as l', 't.LocationID = l.LocationID');
    $this->db->where("Cast(t.DateTime as date) >= '$start_date'");
    $this->db->where("Cast(t.DateTime as date) <= '$end_date'");
    $query = $this->db->get();

    if ($query->num_rows() > 0) 
    {
        return $query->result_array();
    }

当我尝试在End Date 中输入此输入时。

我的意见: '; Truncate Table systemusers; #

给我这个错误:

您的 SQL 语法有错误;检查手册 对应于您的 MariaDB 服务器版本,以便使用正确的语法 near '截断表系统用户; #'' 在第 6 行

选择t.DateTime, su.FirstName, su.MiddleName, su.LastName, l.Description, t.Temperature, t.UnitID, t.Humidity FROM temperaturelogs as t JOIN systemusers as su ON su.UserID = t.Encoder JOIN locations as l ON t.LocationID = l.LocationID WHERE Cast(t.DateTime 作为日期) >= '2016-03-21' AND Cast(t.DateTime 作为日期)

该错误与我的问题没有任何关系...

【问题讨论】:

  • 你使用的是哪个 CI 版本echo CI_VERSION;
  • @Viral 我正在使用最新版本。

标签: php mysql codeigniter


【解决方案1】:

试试,

$this->db->where('Cast(t.DateTime as date) >=', $start_date);
$this->db->where('Cast(t.DateTime as date) <=', $end_date);

【讨论】:

    【解决方案2】:

    在 where 子句中使用 $this-&gt;db-&gt;escape($start_date);

    关注SQL query escaping + codeigniter

    你的活动查询会变成这样

    $this->db->select('t.DateTime,
                           su.FirstName,
                           su.MiddleName,
                           su.LastName,
                           l.Description,
                           t.Temperature,
                           t.UnitID,
                           t.Humidity');
        $this->db->from('temperaturelogs as t');
        $this->db->join('systemusers as su', 'su.UserID = t.Encoder');
        $this->db->join('locations as l', 't.LocationID = l.LocationID');
        $this->db->where("Cast(t.DateTime as date) >= '".$this->db->escape($start_date)"'");
        $this->db->where("Cast(t.DateTime as date) <= '".$this->db->escape($end_date)"'");
        $query = $this->db->get();
    

    【讨论】:

    • 但我使用的是Active Record
    • 没关系,你仍然可以在其中使用变量。我已更新我的回复以包含您的查询。
    • 但我使用$this-&gt;input-&gt;post().. 这不会逃避日期吗?
    • 好吧,语法错误说明了其他问题。无论如何,我认为您还应该对数据类型进行额外的验证。 $start_date/$end_date 在这种情况下,在您直接在活动查询中使用它之前。
    • 是的...但这解决了我的问题...我认为$this-&gt;input-&gt;post() 转义了输入。
    【解决方案3】:

    CodeIgniter 提供您使用以下给定方法在 sql where 子句中设置自定义键/值方法。

    您可以在第一个参数中包含一个运算符以控制比较:

    $this->db->where('name !=', $name);
    $this->db->where('id // 产生:WHERE name != 'Joe' AND id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多