Solutions for filtering invalid characters in php

Source: Internet
Author: User
For errors in Querying SQL statements that are invalid characters in php, filter out & nbsp; & #39; & nbsp ;%& nbsp ;? The mysql_real_escape_string function is displayed on the internet. I will return null after use. why? Is there any useful php character filter function ?, Addcslashes, note that the difference between a C and a php filter for invalid characters
An error occurred while Querying SQL statements. '%?' is filtered out? The mysql_real_escape_string function is displayed on the internet. I will return null after use. why? Is there any useful php character filter function?

------ Solution --------------------
Addcslashes
------ Solution --------------------
Note: The difference is a C

Addcslashes
Escape characters in strings using backslash (PHP 4, PHP 5) as C)

String addcslashes (string str, string charlist)

Description
Returns a string that is prefixed with a backslash before the character in the charlist parameter. This function escapes \ n, \ r, and so on. As in C, ASCII characters lower than 32 and higher than 126 are converted to octal characters.

Be careful when you choose to escape characters 0, a, B, f, n, r, t, and v, they are converted to \ 0, \ a, \ B, \ f, \ n, \ r, \ t, and \ v. In PHP, only \ 0 (NULL), \ r (carriage return), \ n (linefeed), and \ t (separator) are predefined escape sequences. in C, all the converted characters above are predefined escape sequences.

The charlist parameter, for example, "\ 0 .. \ 37", will escape all characters with ASCII codes between 0 and 37.


Use a backslash to reference a string (PHP 3, PHP 4, PHP 5)

String addslashes (string str)

Description
Returns a string that requires a backslash before certain characters for database query statements. These characters are single quotation marks ('), double quotation marks ("), backslash (\), and NUL (NULL ).

An example of using addslashes () is when you want to input data into the database. For example, insert the name 'Reilly into the database, which requires escaping. Most databases use \ as the escape character: O \ 'Reilly. In this way, the data can be put into the database without inserting additional \. When the PHP command magic_quotes_sybase is set to on, it means that when 'is inserted,' is used for escape.

By default, the PHP command magic_quotes_gpc is on, which automatically runs addslashes () on all GET, POST, and COOKIE data (). Do not use addslashes () for strings that have been escaped by magic_quotes_gpc, because this causes double-layer escape. In this case, you can use the get_magic_quotes_gpc () function for detection.

I recommend the latter

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.