What does FILTER_SANITIZE_STRING do?

According to PHP Manual: Strip tags, optionally strip or encode special characters. According to W3Schools: The FILTER_SANITIZE_STRING filter strips or encodes unwanted characters. This filter removes data that is potentially harmful for your application. It is used to strip tags and remove or encode unwanted characters. Now, that doesn’t tell us much. Let’s go see … Read more

string sanitizer for filename

Making a small adjustment to Tor Valamo’s solution to fix the problem noticed by Dominic Rodger, you could use: // Remove anything which isn’t a word, whitespace, number // or any of the following caracters -_~,;[](). // If you don’t need to handle multi-byte characters // you can use preg_replace rather than mb_ereg_replace // Thanks … Read more

How can sanitation that escapes single quotes be defeated by SQL injection in SQL Server?

There are a few cases where this escape function will fail. The most obvious is when a single quote isn’t used: string table= “\”” + table.Replace(“‘”, “””) + “\”” string var= “`” + var.Replace(“‘”, “””) + “`” string index= ” ” + index.Replace(“‘”, “””) + ” ” string query = “select * from `”+table+”` where … Read more

Can I protect against SQL injection by escaping single-quote and surrounding user input with single-quotes?

First of all, it’s just bad practice. Input validation is always necessary, but it’s also always iffy. Worse yet, blacklist validation is always problematic, it’s much better to explicitly and strictly define what values/formats you accept. Admittedly, this is not always possible – but to some extent it must always be done. Some research papers … Read more

How to escape strings in SQL Server using PHP?

addslashes() isn’t fully adequate, but PHP’s mssql package doesn’t provide any decent alternative. The ugly but fully general solution is encoding the data as a hex bytestring, i.e. $unpacked = unpack(‘H*hex’, $data); mssql_query(‘ INSERT INTO sometable (somecolumn) VALUES (0x’ . $unpacked[‘hex’] . ‘) ‘); Abstracted, that would be: function mssql_escape($data) { if(is_numeric($data)) return $data; $unpacked … Read more