Phpget_magic_quotes_gpc () function use _ MySQL

Source: Internet
Author: User
Phpget_magic_quotes_gpc () function uses the magic_quotes_gpc function in php to determine the data prompted by the user, such as adding the escape character "/" to the data from post, get, and cookie. to ensure that the data will not cause the program, especially the database statements may cause fatal errors due to pollution caused by special characters.

In the case of magic_quotes_gpc = On, if the input data has

Single quotation marks ('), double quotation marks ("), backslash (/), and NULL (NULL) are all added with a backslash. These escaping are required. if this option is off, we must call the addslashes function to add escaping to the string.

It is precisely because this option must be On, but there is a conflict between the user's configuration. after this option is deleted in PHP6, all programming needs to be done under magic_quotes_gpc = Off. In such an environment, if user data is not escaped, the consequences are not just program errors. Similarly, it may cause the risk of database injection attacks. Therefore, from now On, you should not rely On this setting to be On any more, so that one day your server will need to be updated to PHP6 and your program will not work properly.

The code is as follows:

When magic_quotes_gpc = On, the get_magic_quotes_gpc () function returns 1.

When magic_quotes_gpc = Off, the get_magic_quotes_gpc () function returns 0.

Therefore, the function get_magic_quotes_gpc () is used to obtain the value of the environment variable magic_quotes_gpc. Since the magic_quotes_gpc option is deleted in PHP6, this function will no longer exist in PHP6.


Php checks whether the get_magic_quotes_gpc function is enabled, so that we can determine whether to use the addslashes function.

The code is as follows:

Function SQLString ($ c, $ t ){
$ C = (! Get_magic_quotes_gpc ())? Addslashes ($ c): $ c;
Switch ($ t ){
Case 'text ':
$ C = ($ c! = '')? "'". $ C. "'": 'null ';
Break;
Case 'Search ':
$ C = "'%". $ c. "% '";
Break;
Case 'int ':
$ C = ($ c! = '')? Intval ($ c): '0 ';
Break;
}
Return $ c;
}

Best practices for preventing database attacks

The code is as follows:

Function check_input ($ value)

{

// Remove the slash

If (get_magic_quotes_gpc ())

{

$ Value = stripslashes ($ value );

}

// If it is not a number, enclose it with quotation marks.

If (! Is_numeric ($ value ))

{

$ Value = "'". mysql_real_escape_string ($ value). "'";

}

Return $ value;

}

$ Con = mysql_connect ("localhost", "hello", "321 ″);

If (! $ Con)

{

Die ('could not connect: '. mysql_error ());

}

// Perform a safe SQL statement

$ User = check_input ($ _ POST ['user']);

$ Pwd = check_input ($ _ POST ['pwd']);

$ SQL = "SELECT * FROM users WHERE

User = $ user AND password = $ pwd ";

Mysql_query ($ SQL );

Mysql_close ($ con );

?>

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.