The most comprehensive approach to preventing SQL injection

Source: Internet
Author: User

The most comprehensive approach to preventing SQL injection

Release time: 2012-04-5 Views: 19361 Category: PHP tutorial

(1) mysql_real_escape_string--escapes special characters in strings used in SQL statements and takes into account the current character set of the connection

Here's how to use it:

    1. $sql = "SELECT COUNT (*) as Ctr from users where username
    2. = ' ". mysql_real_escape_string($username)."' and
    3. Password= ' ". mysql_real_escape_string($pw)." ' Limit 1 ';

mysql_real_escape_string()you can avoid any malicious SQL injection in user input by using a wrapper that is entered as a user.

(2) Open MAGIC_QUOTES_GPC to prevent SQL injection

There is a setting in php.ini: MAGIC_QUOTES_GPC = Off
This is off by default, and if it is turned on, it will automatically convert the query that the user commits to SQL.
For example, "switch to \" And so on, to prevent SQL injection has a major role.

If Magic_quotes_gpc=off, use the addslashes () function

(3) Custom functions

    1. function Inject_check($sql _str) {
    2. return eregi(' select|insert|and|or|update|delete|\ ' |\/\*|\*|\.\.\/|\.\/|union|into|load_file| OutFile ', $sql _str);
    3. }
    4. function verify_id($id=null) {
    5. if(! $id) {
    6. exit(' no submit parameter! ');
    7. } ElseIf(inject_check($id)) {
    8. exit(' argument submitted is illegal! ');
    9. } ElseIf(! Is_numeric($id)) {
    10. exit(' argument submitted is illegal! ');
    11. }
    12. $id = intval($id);
    13. return $id;
    14. }
    15. function Str_check( $str ) {
    16. if(! GET_MAGIC_QUOTES_GPC()) {
    17. $str = addslashes($str); //Filter
    18. }
    19. $str = str_replace("_", "\_", $str);
    20. $str = str_replace("%", "\%", $str);
    21. return $str;
    22. }
    23. function Post_check($post) {
    24. if(! GET_MAGIC_QUOTES_GPC()) {
    25. $post = addslashes($post);
    26. }
    27. $post = str_replace("_", "\_", $post);
    28. $post = str_replace("%", "\%", $post);
    29. $post = nl2br($post);
    30. $post = htmlspecialchars($post);
    31. return $post;
    32. }

The most comprehensive method of preventing SQL injection

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.