dseigqk7443 2016-02-20 19:31
浏览 34

在Codeigniter中处理用户输入的最佳实践

I wonder what is the best and the most secured way of handling user's input in Codeigniter. Basically I have form for user's profile made by form helper like this:

echo form_open();
    echo form_label($this->lang->line('user_update_profile_first_name'), 'first_name');
    echo form_input(array('type' => 'text', 'name' => 'first_name', 'id' => 'first_name', 'maxlength' => '255', 'required' => 'true', 'value' => set_value('first_name', $user_profile['first_name'], false)));

    echo form_label($this->lang->line('user_update_profile_last_name'), 'last_name');
    echo form_input(array('type' => 'text', 'name' => 'last_name', 'id' => 'last_name', 'maxlength' => '255', 'required' => 'true', 'value' => set_value('last_name', $user_profile['last_name'], false)));

    echo form_label($this->lang->line('user_update_profile_birth_date'), 'birth_date');
    echo form_input(array('type' => 'text', 'name' => 'birth_date', 'id' => 'birth_date', 'maxlength' => '255', 'required' => 'true', 'value' => set_value('birth_date', $user_profile['birth_date'], 

    echo form_submit(array('value' => $this->lang->line('user_update_profile_form_submit'), 'name' => 'submit', 'class' => 'btn btn-primary'));
echo form_close();

As you can see in my code I am skipping xss filtering provided in set_value function due to xss filtering is done in form_input() already.

My Controller function for inserting data in DB looks like this

$validation_rules = array(
    array(
        'field' => 'first_name',
        'label' => $this->lang->line('user_update_profile_validation_error_first_name'),
        'rules' => 'required|trim|max_length[255]'
    ),
    array(
        'field' => 'last_name',
        'label' => $this->lang->line('user_update_profile_validation_error_last_name'),
        'rules' => 'required|trim|max_length[255]'
    ),
    array(
        'field' => 'birth_date',
        'label' => $this->lang->line('user_update_profile_validation_error_birth_date'),
        'rules' => 'required|trim|max_length[255]'
    )
);

$this->form_validation->set_rules($validation_rules);
if($this->form_validation->run()) {
    $user_data = array(
        'user_id' => $this->profile_data->user_id,
        'first_name' => $this->input->post('first_name', TRUE),
        'last_name' => $this->input->post('last_name', TRUE),
        'birth_date' => date('Y-m-d',strtotime($this->input->post('birth_date', TRUE)))
    );

    if($this->user_model->update_user_profile($user_data)) {
        $view_data['success'] = TRUE;
        $new_site_language = $this->language_model->getLanguageFolderById($user_data['site_language']);
        $this->lang->load('application/user_lang', $new_site_language);

    } else {
        $view_data['server_error'] = TRUE;
    }
}

I am filtering here data from user by provided $this->input->post('', true) xss filter. In model I am inserting data to DB by active record class. I am just wondering if this is the right and secure way of handling users input if there is not needed something like htmlspecialchars() . But what happens when someone have some "special" chars in name like for example Someone O'Sombody or some names from foreign countries? I am also showing data in navbar using html_escape($this->profile_data->first_name) to prevent running users potentially dangerous code. Did I get this whole "security thing" in the right way or there should be something changed because of potential danger?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 @microsoft/fetch-event-source 流式响应问题
    • ¥15 ogg dd trandata 报错
    • ¥15 高缺失率数据如何选择填充方式
    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False