PHP using addslashes function to implement SQL anti-injection example

Source: Internet
Author: User
We know that the role of the addslashes () function is to add backslashes before some of the predefined characters in the input string. So how does it relate to our anti-SQL injection? What is SQL injection?

SQL injection attacks are the most common means by which hackers attack websites. If your site does not use strict user input validation, it is often vulnerable to SQL injection attacks. SQL injection attacks typically submit bad data or query statements to the site database

, it is likely that the records in the database will be exposed, changed, or deleted. This article mainly introduces PHP using addslashes function to implement SQL anti-injection example , through The example describes the use of the addslashes function for SQL anti-injection.

Example

Parameter ' a '. Z ' defines that all uppercase and lowercase letters are escaped and the code is as follows:

echo addcslashes (' foo[] ', ' A. Z '); Output: foo[] $str = "Is your name O ' Reilly?"; Defines the string, which includes the character that needs to be escaped echo addslashes ($STR);  Outputs an escaped string

Definition and Usage:

The Addslashes () function adds a backslash before the specified predefined character.

These predefined characters are: Single quotation mark ('), double quotation mark ("), backslash (), NULL

Grammar:

Addslashes (String)

Of course this function is more secure and the instance code is as follows:

$str = "<a href= ' test ' >test</a>"; Defines a string that contains special characters $new =htmlspecialchars ($str, ent_quotes);  Perform the conversion operation Echo $new;           Output conversion result//Output $STR = "Jane & ' Tarzan '";  Defines an HTML string echo html_entity_decode ($STR);   Output the converted content echo "<br/>"; Echo Html_entity_decode ($str, ent_quotes); Content with optional parameter output

This article has a good reference value for PHP security program design. But using addslashes to prevent SQL injection hackers is a way to bypass, and not so secure. Many PHP programmers still rely on addslashes to prevent SQL Note

, it is recommended to strengthen Chinese to prevent SQL injection check. The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, and addslashes just modifies 0xbf27 to 0xbf5c27 to become a valid multibyte character,

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.