Copying online examples like:
$res = mysql_query( "select from persons where name = $name and password = $password" );
Or things line:
$output = "<b>$name</b>";
Or even:
$email = "From: <$from>\n"
. "To: <$to>\n"
. "Subject: $subject"
Basically, everywhere you combine strings from multiple sources, the variables need to be escaped. That includes:
- SQL queries
- HTML strings
- url parameters
- regexps
- generated JavaScript
- shell commands
- email headers
- outputted http headers,
and so on.
Otherwise, You can easily login with strings like 'password' or 1 --
, inject HTML to read user login cookies, or add new SMTP/HTTP headers by adding a newline character in the input..
As solution, there is:
mysql_real_escape_string()
htmlentities()
urlencode()
preg_quote()
json_encode()
escapeshellarg()
Ideally, for database queries I strongly suggest to avoid the mysql_...() functions directly, and use - or build - a wrapper library around it. Something which offers an API line:
$rs = $db->select( "select from persons where name = ? and password = ?", array( $name, $password ) )
foreach( $rs as $record ) // $rs is an object which implements the Iterator interface
{
...
}