PART 1 - magic quotes
basic idea is that if 'magic quotes' is on then PHP will add backslashes to ' and " when it is getting it from GET, COOOKIE, or POST (that's what the 'gpc' in 'get_magic_quotes_gpc()' stands for.
What this means is that when users POST or GET a form to you, your server might add backslashes to those particular chars.
PART 2 - Why??
Because PHP was designed with some shortcuts for dumb people (not you) to get started quickly and somebody thought it would be convenient to auto-add backslashes so the user didn't have to before creating a query. If you are trying to insert a string with quotes into a database, you can probably see why this wouldn't be valid sql:
$sql = "INSERT INTO table SET name='O'Malley'";
that apostrophe in O'Malley breaks the SQL. so you would put a backslash before it to fix it.
PART 3 - detecting and getting rid of those stupid, useless and totally non-magical 'magic_quotes'
this line is good...
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
it will remove slashes that are added by magic quotes, but NOT ones that might have been entered by the user....like when i sit here and type \'
I want it in my post here as an example to describe something to you.
It will also not bother removing magic_quotes if your php install DOESN'T HAVE THEM TURNED ON.
stripslashes() is effective for removing magic quotes. so that kind of line is good.
PART 4 - Escaping strings properly before putting into SQL
Why use mysql_real_escape_string() instead of addslashes?
BECAUSE, depending on what language settings your MySQL installation has going on, there are OTHER LANGUAGE SYMBOLS THAT MYSQL TREATS AS DOUBLE OR SINGLE QUOTES. There's that angle quote that phpMyAdmin uses all the time: `(shares a key with the tilde char up by your escape key).
There are others like curly quotes left and right. curly double quotes left and right, etc. Depending on what language setting your MySQL has, there could be a dozen for all I know. addslashes() doesn't affect those at all as far as i know. mysql_real_escape_string should - i think so anyway.
Remove magic quotes (if the setting is turned on) and use mysql_real_escape_string instead of addslashes.
PART 5 - Don't stripslashes unnecessarily
This might be kind of confusing, but when you escape all those quote marks when you are creating your SQL, don't worry! The contents of your database SHOULD NOT HAVE ANY SLASHES THAT NEED TO BE STRIPPED IF YOU HAVE DONE YOUR WORK PROPERLY. For this reason, you should NOT stripslashes() when you retrieve information from your database. If you are getting all kinds of when you pull data from your database, you didn't insert it properly in the first place.
Again, The reason that you add the slashes in the first place is to make valid SQL. When you run that valid SQL, the backslashed quote marks don't go into the database with the backslashes. You just backslash them so that the SQL isn't broken. Is that clear? Look at the O'Malley example above. What I typed there is not valid SQL. If I 'escaped' that one quote in O'Malley then it would be valid SQL
$name = "O'Malley";
// or i think i could to this...
$name = 'O\'Malley'; // notice the backslash...the var still just contains O'Malley...we just have to escape the quote to make valid php...the idea for sql is the same
// this line will add a backslash to O'Malley but that's just so the SQL isn't broken when we try to run the query...it doesn't actually go into the database
$sql = "INSERT INTO table SET name='" . mysql_real_escape_string($name) . "' ";
mysql_query($sql)
If I were to retrieve that record and echo the name value from it, it would be O'Malley (without backslashes).
gosh that is a lot of typing. hope this is clear.