I'm having some problems understanding when to use or not use addslashes() and stripslashes() functions.
For example, in http://docs.moodle.org/en/Development:Coding , point 15, it says:
Incoming data from the browser (sent via GET or POST) automatically has magic_quotes applied (regardless of the PHP settings) so that you can safely insert it straight into the database. All other raw data (from files, or from databases) must be escaped with addslashes() before inserting it into the database. Because this is so often done incorrectly, there is more explanation on this issue of adding and stripping slashes on a separate page.
In that separate page ( http://docs.moodle.org/en/Developer:Slashes ) it says something like:
Neither
stripslashes()
nor addslashes()
should be used when going from User Input to the Database or from the Database to Screen Output (black arrows) but stripslashes()
should be used when displaying user input on the screen (red arrow) and addslashes()
should be used when reinserting data in to the database that came from the database (blue arrow). These last two are rare, so the use of addslashes and stripslashes should be rare.Due to 99% of the code i'm touching is dealing with UserInput (optional_param() etc) «» DataBase (Moodle's DB API), does this mean i don't need to use ever addslashes() and stripslashes() in my code?
For example, i'm getting via GET a string called a'a"a which i need to store in a DDBB field of a certain table, and i belive the right thing to do is to store it as a\'a\"a to avoid security attacks (SQL injections and stuff). Is this right?
So, for this I use a form and urlencode() to get the value from the user and then i understand that the browser decodes it automatically (so i don't need to use urldecode() ever), and after that i use optional_param() to capture that value, which i store in a variable and finally i insert it to the DDBB with insert_record() moodle function from dmllib.php. The other way around, i use something that calls get_record_select() to read that value from DDBB.
In this "workflow" (huh) i don't know if i should use addslashes() before saving to DDBB and stripslashes() after reading from DDBB.
Excuse my english and thanks.