【问题标题】:Codeigniter escape - unescape data from databaseCodeigniter 转义 - 从数据库中转义数据
【发布时间】:2012-04-11 14:21:38
【问题描述】:

请不要认为通过阅读问题来否定问题。 因为我进行了很多搜索,而且我知道这个问题到处都被问了很多但是我不知道我没有得到正确的解决方案。

我希望数据在存储在数据库中和从数据库中检索时是安全的(来自 sql 注入和 xss )。

我的 CI 配置设置:

$config['global_xss_filtering'] = FALSE;

我的数据库添加查询:

$add_data['name'] = $this->db->escape($name);
.....
.....
$this->db->insert($this->table, $add_data);

我的数据库视图查询:

$q =$this->db->select($this->fld);
$q = $this->db->where("$this->cond");
$q = $this->db->order_by($this->sortid, $this->sortby);
$q = $this->db->limit( $this->limit,$this->offset);
$ret=$q->get()->result_array();

我的问题是:

DB 值带有单引号:例如。如果 $name = abc 在 DB 它是 : 'abc'

问题:

如果我想显示带引号的数据,如何在不带引号的情况下显示它们。

加号

如何防止在数据库中添加单引号。

【问题讨论】:

    标签: database codeigniter quotes


    【解决方案1】:

    使用CI insert 时不需要转义数据。活动记录会自动为您转义值。在您的示例中:

    /* $add_data['name'] = $this->db->escape($name); - not need */
    $add_data['name'] = $name;
    .....
    .....
    $this->db->insert($this->table, $add_data);
    

    阅读CI Active Records。每个功能都有解释。还有哪个需要转义,哪个不需要

    【讨论】:

    • 但如果我不添加 $this->db->e​​scape 那么它不会添加斜杠。实际需要的时候。前任。 $add_data['name'] = 嫁给 X'Mas
    【解决方案2】:

    活动记录自动转义值。对于显示不带引号的字符串,您可以使用字符串助手。

    $string="Joe's \"dinner\"";
    $string=strip_quotes($string); //results in "Joes dinner"
    

    它在User's Guide

    【讨论】:

    • 别忘了加载字符串助手:$this->load->helper('string');
    • 不工作... 例如。我的值是:AA'BB"CC\DD/EEFF;GG~HH in Add db->escape is used. in DB it is showing : 'AA\'BB\"CC\\DD/EEFF;GG~HH' 在显示中:使用strip_quotes。在显示中是:'AA'BB"CC\DD/EE`FF;GG~HH '
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多