In CodeIgniter, how can I avoid sql injection? Is there any method to set in config file to avoid sql injection? I am using this code for selecting values:
$this->db->query("SELECT * FROM tablename WHERE var='$val1'");
and this for inserting values:
$this->db->query("INSERT INTO tablename (`var1`,`var2`) VALUES ('$val1','$val2')");
Another method used to insert and select values from the database is CodeIgniter's insert()
and get()
methods. Is any chance to sql injection while using CodeIgniter's bulit-in functions
In CodeIgniter: There are 2 action to prevent SQL Injection. For those who are novelty in web programming, another kind of security hole in web programming which can be fatal because it can expose your inner side of application’s database, it is SQL Injection.
And thankfully again, Codeigniter has capability to deal with it. But unfortunately, many of CI programmer I collaborated (and even you) did (or might) forget this two action to prevent any circumstances of SQL injection.
Stick with ActiveRecord capability The first thing is do not in any circumstances dealing with querying the data by using full query like this :
You don’t know what exactly inside $user or $password variable when it comes to user who will do deliberately the wrong thing. Even XSS sanitiser won’t deal with someone who inputs combination of quote, semicolon or dash character in it. So in this case, you need to learn this Active Record thing because it has input sanitiser capability dedicated to prevent SQL injection. And don’t worry, it support kind of function chaining like this :
But remember, it won’t work if you still do combining usual (partially) query function inside of active record function like this :
Which actually could be changed like this.
The point is, use every bit of possibility of CodeIgniter’s Active Record and don’t mess with it.
But If that ain’t work, there is an alternative If you have a very long query and don’t bother to convert it to Active Record’s style, you can sanitised your input manually by using this function :
// For use inside LIKE query
And you can safely concatenate the sanitised/escaped input inside your query.
You can check if you var contain only letters of numbers, meaning you var mast be in you defined format. before you insert it into the query
While accepting value from client side, Better to use this code,
While inserting better to use codeigniter inserting method,
When using this method codeingniter automatically do all escape so we no need do escape manual.
You should try to avoid writing your queries directly into a string and then passing them to the query function. A better option would be to use the Active Record class which will build your queries for you and escape the values. http://codeigniter.com/user_guide/database/active_record.html
If you want to avoid using the Active Record class for whatever reason then you can view the Codeigniter documentation for the database class which has an escape method for escaping your values before passing them to the query method. http://www.codeignitor.com/user_guide/database/queries.html
Ben
CodeIgniter's Active Record methods automatically escape queries for you, to prevent sql injection.
or
If you don't want to use Active Records, you can use query bindings to prevent against injection.
Or for inserting you can use the
insert_string()
method.There is also the
escape()
method if you prefer to run your own queries.you can use
method..
other methods are listed here.
http://codeigniter.com/user_guide/database/queries.html