GET_MAGIC_QUOTES_GPC () function description in PHP

Source: Internet
Author: User

Introduction to GET_MAGIC_QUOTES_GPC Functions

Get PHP environment variable MAGIC_QUOTES_GPC value, belong to PHP system function.
Syntax: Long get_magic_quotes_gpc (void);
Return value: Long integer
This function obtains the PHP environment configuration variable MAGIC_QUOTES_GPC (GPC, Get/post/cookie) value. A return of 0 indicates that this feature is turned off; 1 indicates that this feature is turned on.


When MAGIC_QUOTES_GPC is open, all ' (single quotes), "(double quotes), (backslash) and null characters are automatically converted to overflow characters that contain backslashes.
The MAGIC_QUOTES_GPC setting automatically adds a backslash to the ' "in the data that is coming from the GPC (Get,post,cookie). You can use GET_MAGIC_QUOTES_GPC () to detect system settings.
If this setting is not turned on, you can add it by using the addslashes () function, which adds a backslash to the needs of the database query statement, and so on.
These characters are single quotes ('), double quotes ("), backslashes () and NUL (NULL characters).
By default, the PHP instruction MAGIC_QUOTES_GPC is on, and it automatically runs Addslashes () for all get, POST, and COOKIE data.

Do not use Addslashes () on strings that have been escaped by MAGIC_QUOTES_GPC, because this can result in a double escape. You can use the function GET_MAGIC_QUOTES_GPC () for instrumentation when this situation is encountered.

Cases

The correct approach to using GET_MAGIC_QUOTES_GPC () to prevent database attacks

The code is as follows Copy Code

<?php
function Check_input ($value)
{
Remove Slash
if (GET_MAGIC_QUOTES_GPC ())
{
$value = Stripslashes ($value);
}
Add quotes if not numbers
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 ());
}
To secure the SQL
$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);
?>

Summarized as follows:

1. For the magic_quotes_gpc=on situation,

We can not make string data for input and output databases
Addslashes () and Stripslashes (), the data is also displayed correctly.

If you addslashes () the input data at this time,
Then you must use Stripslashes () in the output to remove the extra backslash.

2. For the Magic_quotes_gpc=off situation

You must use Addslashes () to process the input data, but you do not need to use stripslashes () to format the output
Because Addslashes () did not write the backslash to the database, it only helped MySQL complete the execution of the SQL statement

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.