我的codeigniter自动完成与阿贾克斯我的、自动完成、codeigniter、阿贾克斯

2023-09-11 01:08:26 作者:無敵帥鍋╮

我添加私人信息到我的网站。在我的表单收件人文本字段,我想提出有效的用户名,当有人开始打字。阅读教程和研究一些剧本后,我做了如下code从我的数据库表中指定用户提示用户名。它的工作原理,但我不能确定如何正确,安全的很。

jQuery的(使用jQuery UI自动完成插件):

  $(函数(){
    $(#username).autocomplete({// id为#username收件人文本字段
        来源:功能(请求,响应){
            $阿贾克斯({
                网址:HTTP://localhost/mysite/index.php/my_controller/search_username
                数据类型:JSON,
                数据:请求,
                成功:功能(数据){
                    如果(data.response =='真'){
                       响应(data.message);
                    }
                }
            });
        },
        的minLength:1,
        选择:功能(事件,UI){
            //做一些额外的选择。也许用户ID添加到隐藏的输入
        },

    });
});
 

控制器(为简单起见我没有使用一个模型,虽然我计划)

 函数search_username()
{
        $用户名=修剪($这个 - >输入 - >获得('术语')); //获得通过文本字段中发送字参数。不知道安全怎么得到()是

        $这个 - > DB->选择(ID,用户名');
        $这 - > DB-肽从('用户');
        $这个 - > DB->像(用户名,$用户名);
        $这个 - > DB->极限('5');
        $查询= $这个 - > DB->获得();

        如果($查询 - > NUM_ROWS()大于0)
        {
            $数据['回应'] =真; //如果用户名存在设置为true
            $数据['消息'] =阵列();

            的foreach($查询 - >的结果()为$行)
            {
                $数据[信息] [] =阵列(
                    标签=> $行向>的用户名,
                    '值'=> $行向>的用户名,
                    USER_ID'=> $行向> ID
                );
            }
        }
        其他
        {
            $数据['回应'] ='假'; //设置为false,如果用户不合法
        }

        回声json_en code($的数据);
}
 

解决方案

有一个编辑,我会建议做...

我将使XSS保护通过传递第二个参数 TRUE 的get()

  $用户名=修剪($这个 - >输入 - >获得('术语',TRUE));
 
荷甲联赛直接结束,阿贾克斯排名第一获得欧冠附加赛资格

I’m adding private messaging to my site. In the Recipient text field in my form, I want to suggest valid usernames when someone starts typing. After reading tutorials and studying some scripts I made the following code for suggesting usernames from my database table named users. It works but I’m not certain how correct and secure it is.

Jquery (using the Jquery UI autocomplete plugin):

$(function() {                     
    $( "#username" ).autocomplete({ //the recipient text field with id #username
        source: function( request, response ) {
            $.ajax({
                url: "http://localhost/mysite/index.php/my_controller/search_username",
                dataType: "json",
                data: request,
                success: function(data){
                    if(data.response == 'true') {
                       response(data.message);
                    }
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
            //Do something extra on select... Perhaps add user id to hidden input    
        },

    });
}); 

Controller (for simplicity I did not use a model although I plan to)

function search_username()
{
        $username = trim($this->input->get('term')); //get term parameter sent via text field. Not sure how secure get() is

        $this->db->select('id, username'); 
        $this->db->from('users');
        $this->db->like('username', $username);
        $this->db->limit('5');
        $query = $this->db->get();

        if ($query->num_rows() > 0) 
        {
            $data['response'] = 'true'; //If username exists set true
            $data['message'] = array(); 

            foreach ($query->result() as $row)
            {
                $data['message'][] = array(  
                    'label' => $row->username,
                    'value' => $row->username,
                    'user_id'  => $row->id
                );
            }    
        } 
        else
        {
            $data['response'] = 'false'; //Set false if user not valid
        }

        echo json_encode($data);
} 

解决方案

There is one edit that I would recommend making...

I would enable XSS protection by passing a second argument TRUE to get()

    $username = trim($this->input->get('term', TRUE));