LoginSignup
1
2

More than 5 years have passed since last update.

codeigniterのエスケープ

Posted at

codeigniterのform_helperとset_value()

codeigniterのviewページを作っていた時にset_value()の挙動が気になったのでメモ

set_value()

いい感じにPOSTパラメータを保持してフォームやテキストエリアの値をセットできます

function set_value($field, $default = '', $html_escape = TRUE)
    {
        $CI =& get_instance();

        $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
            ? $CI->form_validation->set_value($field, $default)
            : $CI->input->post($field, FALSE);

        isset($value) OR $value = $default;
        return ($html_escape) ? html_escape($value) : $value;
    }

ここでform_input()などのform_helperと組み合わせて使おうとするとform_helperはデフォルトでエスケープしてくれますので二重にエスケープされます。

ざっくり

$h0 = "<script>alert('1');</script>";
$h1 = html_escape($h0);
$h2 = html_escape($h1);
var_dump($h0, $h1, $h2);

// string(28) "(html要素)" string(50) "<script>alert('1');</script>" string(74) "&lt;script&gt;alert(&#039;1&#039;);&lt;/script&gt;"

こんな具合です。
二重にエスケープされるのを防ぐにはset_valueの第3引数をfalseにすれば行けます。

自己解決したら、日本語ドキュメントに書いてありましたw

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2