: This article mainly introduces the most comprehensive method to prevent SQL injection. if you are interested in the PHP Tutorial, please refer to it. (1) mysql_real_escape_string -- escape special characters in strings used in SQL statements and take into account the connected current character set
The usage is as fo
Problem Description:If the data entered by the user is inserted into an SQL query statement without processing, then the application is likely to suffer a SQL injection attack, as in the following example:?
123
$unsafe_variable= $_POST[‘user_input‘]; mysql_query("INSERT INTO `table` (`column`) VALUES (‘" . $unsafe_variable. "‘)");
using the following: Dim Input,pattern as StringInput=me.txtinput.texttrim ()Pattern=me.txtpattern.textIf re.mathc (Input,pattern). Success then ' uses the match method to validate user-entered content with a defined templateMessageBox.Show ("True,input matches pattern") Else MessageBox.Show ("False,input does not match pattern") End if The above, by looking at. NET Video summary of the three ways to avoid SQL in
Here, I will share with you some examples and experiences of preventing SQL injection attacks summarized by the webmaster. I hope this tutorial will help you.
1. Configure on the server
Security, PHP code writing is one aspect, and PHP configuration is critical.
We manually install php. The default configuration file of php is in/usr/local/apache2/conf/php. ini, we mainly need to configure php. the content
* from test where username ="***" union select * from users/* and password = "***"';
The forward slash * indicates that the subsequent query is not executed. mysql supports union Joint query, so all data is directly queried. Therefore, the universal user name for this SQL statement is: * ** "union select * from users /*
However, this injection only applies to SQL
The difference between # and $ in the MyBatis
1. #将传入的数据都当成一个字符串, a double quotation mark is added to the data that is automatically passed in. For example: ORDER by #user_id #, if the value passed in is 111, then the value that is parsed into SQL will be "111", and if the value passed is an ID, the SQL that is parsed is the order by "id". 2. $ to display incoming data directly in
;setattribute (Pdo::attr_emulate_prepares, false); Disables the emulation effect of the prepared statements
$DBH->exec ("Set names ' UTF8 '");
$sql = "SELECT * from test where name =? and password =?";
$stmt = $dbh->prepare ($sql);
$exeres = $stmt->execute (Array ($testname, $pass));
if ($exeres) {
while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) {
print_r ($row)}
}
$DBH = null; The above code will
Can the following code effectively prevent SQL injection? In general, how does the code below {code...} effectively prevent SQL injection?
How do you do this.
SetAttribute (PDO: ATTR_EMULATE_PREPARES, false); // disable the si
Using Hibernate to prevent SQL injection, hibernatesql
Write the code before, and input an Hql or SQL statement of the String type to the background for execution.
This is actually a stupid practice !!!!For example ~~
We mimic the user login scenario:
A common practice is to dynamically splice the username and password
' = ' A, the code that is actually executed becomes:SELECT * from user table WHERE NAME = ' xxx ' and PASSWORD = ' xxx ' OR ' a ' = ' a ';At this point, the query statement can always query the results. Then the user is directly logged in successfully, obviously we do not want to see such a result, this is the SQL injection problem.To do this, we use PreparedStatement to solve the corresponding problem.API
This article describes how to prevent SQL injection in PHP. For more information, see the following description:
If the data entered by the user is inserted into an SQL query statement without being processed, the application may be vulnerable to SQL
For example:
If your query statement is select * from Admin where username = " user " and Password = " PWD ""
Then, if my user name is: 1 or 1 = 1
Then, your query statement will become:
Select * from Admin where username = 1 or 1 = 1 and Password = " PWD ""
In this way, your query statements are passed and you can access your management interface.
Therefore, you need to check user input for defense purposes. Special characters, such as single quotes, double quotation marks, semicolons, commas,
) Crack Column Name: and (select count (column name) from table name) Returns the correct, then the table or column name is correct Prevention: 1. The user's input isChecksum, you can convert single quotes and double "-" by regular expressions, control data types, and so on. 2. Do not use dynamically assembled SQL, you can useParameterized SQLor directly usingStored ProceduresFor data query Access " Select ID, no from user where id=? " ;
information with the identity information stored on the server.
Because SQL commands have been modified by injection attacks and cannot actually authenticate user identities, the system will incorrectly authorize attackers. If the attacker knows that the application will directly use the content entered in the form for identity authentication queries, he will try to input some special
Do phpselect statements require preprocessing to prevent SQL injection? Does php select statements require preprocessing to prevent SQL injection?
Reply content:
Does php select statements require preprocessing to
Label:Because the user's input might be this:
1
value‘); DROP TABLE table;--
Then the SQL query will become as follows:
1
INSERTINTO`table` (`column`) VALUES(‘value‘); DROP TABLEtable;--‘)
What effective methods should be taken to prevent SQL injection?Bes
First, on the server-side configuration
Security, PHP code writing is on the one hand, PHP configuration is very critical.
Our PHP hand-installed, PHP default profile in/usr/local/apache2/conf/php.ini, our main is to configure the content of PHP.ini, let us execute PHP can be more secure. Security settings throughout PHP are primarily designed to prevent Phpshell and SQL
Method One: Password comparison pair
Thinking: First, the user entered the user name to query the database, get the user name in the database corresponding password, and then the query from the database password and user submitted over the password to carry out the match.
Code:
The code is as follows
Copy Code
$sql = "Select password from users where username= ' $name '"; $res =mysql_query ($
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.