Two seperate Questions here:
First off, assume that we're going to check the query for illiict statements after we form it.
Which of these is the most gerally accepted way to form a query statement in PHP.
$query="SELECT username FROM " . $tblpre . "users WHERE id = '".$_SESSION['id']."' AND anotherval = '".$var."' LIMIT 1";
$query="SELECT username FROM " . $tblpre . "users WHERE id = '$_SESSION[id]' AND anotherval = '$var' LIMIT 1";
//OTHER
Now assume that we're using a specially made query($foo) function in place of mysql_query that will check the passed statement for bad things and handle any error reporting, as well as return the result of the query.
In my experience until recently I always worked on servers where magic_quotes_gpc has been On. Meaning that there was no real reason for me to check the submits.
Now in my stupor, I thought I could do a check like the following inside my query($foo) function.
if(!get_magic_quotes_gpc())
{
$foo=addslashes($foo);
}
In case you missed where this goes wrong, examine the following:
$query="SELECT * FROM " . $tblpre . "photos WHERE id = '$tpi'";
$result=query($query);
//SQL Query: SELECT * FROM tableprefix_photos WHERE id = /'123/'
//Die
//Note: I know the slashes go the other way but the boards aren't letting me put them in
Not a situation I care for. So what would be a solution for checking the completely-formed statement inside the query($foo) function that would be efficient, upwards compatible, and secure?
Please keep in mind the extremely frustrating magic_quotes_sybase setting. The answers given will affect what goes into the Gallery System (see sig) and I'm trying to make it as secure and cross-platform as possible.