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

Do htmlspecialchars and mysql_real_escape_string keep my PHP code safe from injection?

When it comes to database queries, always try and use prepared parameterised queries. The mysqli and PDO libraries support this. This is infinitely safer than using escaping functions such as mysql_real_escape_string. Yes, mysql_real_escape_string is effectively just a string escaping function. It is not a magic bullet. All it will do is escape dangerous characters in … Read more

In PHP when submitting strings to the database should I take care of illegal characters using htmlspecialchars() or use a regular expression?

There are no “illegal” characters for the database. Database that cannot store some characters is a nonsense. There are some service characters, like quotes, used to delimit strings. These characters should be just escaped, not eliminated. To send a query to the database, you have 2 options: Build a query usual way, to make it … Read more

CSRF, XSS and SQL Injection attack prevention in JSF

XSS JSF is designed to have builtin XSS prevention. You can safely redisplay all user-controlled input (request headers (including cookies!), request parameters (also the ones which are saved in DB!) and request bodies (uploaded text files, etc)) using any JSF component. <h:outputText value=”#{user.name}” /> <h:outputText value=”#{user.name}” escape=”true” /> <h:inputText value=”#{user.name}” /> etc… Note that when … Read more

Java – escape string to prevent SQL injection

PreparedStatements are the way to go, because they make SQL injection impossible. Here’s a simple example taking the user’s input as the parameters: public insertUser(String name, String email) { Connection conn = null; PreparedStatement stmt = null; try { conn = setupTheDatabaseConnectionSomehow(); stmt = conn.prepareStatement(“INSERT INTO person (name, email) values (?, ?)”); stmt.setString(1, name); stmt.setString(2, … Read more

Reference: What is a perfect code sample using the MySQL extension? [closed]

My stab at it. Tried to keep it as simple as possible, while still maintaining some real-world conveniences. Handles unicode and uses loose comparison for readability. Be nice 😉 <?php header(‘Content-type: text/html; charset=utf-8’); error_reporting(E_ALL | E_STRICT); ini_set(‘display_errors’, 1); // display_errors can be changed to 0 in production mode to // suppress PHP’s error messages /* … Read more