Although you don't need to write much code, CI provides a lot of the standard functionality and better security, and it remembers all those oddities and quirks. It keeps track of things you may have forgotten about (those little touches that distinguish amateur sites from professional ones).
Keep your links up-to-date automatically
Suppose you've just written a menu page, with lot of hyperlinks to other pages in your site. They are all in the traditional HTML format as shown:
Then, you decide to move the site to another URL. That means you have to go painstakingly through your code, looking for each URL, and rewriting it, else none of your links will work.
CI gives you a simple function to write hyperlinks like this:
CI also encourages you to put the URL of your site in a configuration file that the rest of your site can access. CI's anchor function that we've used here, automatically refers to that configuration file. So, when you come to move your site, you only need to change that one entry in the configuration file, and all your hyperlinks are updated automatically.
Preventing database SQL injection attacks and form prepping
Data entry is fraught with problems. There are certain limitations of HTML and databases, as a result of which data containing symbols such as apostrophes and quotation marks may not be saved correctly, or even worse, your database may be open to malicious attacks.
For example, take this query:
Consider that the variables have the following values:
Now our query would translate to:
This query will return a good result, but, what if our variables were:
Now our query would produce:
This time the variable's data contains a new "where" clause with a condition that is always true. The user inserts some characters, such as " ' " to make our query behave in a way we don't want, and give bad results. It's easy to see that with this kind of attacks more than just giving bad results can be achieved, dropping tables being one of the worse things. These problems don't always come in the shape of SQL injection attacks; most of the time not prepping data correctly would bring problems too, for example:
Our password variable contains a password that looks quite secure, but will produce a problem in our query:
The data will cut the query, producing some errors when executed. What can we do to prevent these problems? Well the answer to this is to prepare or "prep" our data in our data entry form, before it is submitted to the database. All this takes time and a certain amount of extra coding.
CI's form helper does this, automatically. So, when you create an input box by typing:
You're also getting the hidden benefit of:
This is the code that handles special characters such as "&" so that they don't cause confusion while your form is being submitted. As you can see, there is some quite tricky regex code in there.
Possibly you like typing regexes. Some people like lying on a bed of nails, some like listening to ABBA; it's a free country. If you don't like these things, you can let CI do them for you (the regexes, not ABBA), and you needn't even be aware of the code that's working in the background for you, every time you write that one simple line of code:
Besides this, CI's Active Record class automatically escapes special characters in database queries; this can also be achieved with query bindings, to give some extra automatic protection to your site; without our doing anything CI is helping us to make our site more secure.
Protect your site from XSS attacks
As stated on Wikipedia (http://en.wikipedia.org/wiki/Cross-site_scripting), XSS (cross site scripting) is a kind of vulnerability that allows some unwanted code to be executed in our application, phising attacks, data theft, and more. In order to avoid this you should validate your data.
CodeIgniter helps you to do so, in all your applications if you set global XSS filter to true in your configuration file, or whenever you need it:
You can even use it to check potential XSS attacks within image files:
The second parameter tells CI that it is an image that needs validation.