I’ve used PHP for years but have only recently become aware of what magic quotes is and does and also that once PHP 6 arrives to our hosters we will no longer be able to use it. Can I please check that I understand it correctly?
With magic quotes on you don’t need to escape anything as it’s done automatically on all GET, POST and COOKIE data.
With magic quotes off you should escape everything before performing a database query. You should use a database-specific function if possible (mysql_real_escape_string) but if not addslashes will do.
So I have a question (a few actually):
With no magic quotes, how to we handle the reading and writing of text files?
When do you think most hosters will install PHP 6?
Are you going to charge your clients to make all your sites PHP 6-friendly?
Just to clarify though if magic quotes is on and your using a GET or POST variable in a MySQL query you don’t need addslashes or mysql_real_escape_string?
If magic quotes is on, then you should disable it, as per this example. Then you can addslashes (bad idea), or real_escape_string (better) as you need.
The “real” on mysql_real_escape_string is there for a reason.
Edit, to actually answer your qn, you can’t use those functions if magic_quotes is on, they will escape the escaped data and you will end up with strings like 'can\\\‘t’ instead of 'can\‘t’. Essentially magic quotes just runs everything through addslashes, which is too basic a function to use generically.
mysql_real_escape_string() is completely unnecessary once you start using the more modern ways of coding the SQL that keeps the SQL and the data completely separate.